swift - iOS: Core Data light weight migration is throwing error -
in 1.0 version of app, have created core data db named "myapp.sqlite". when core data light weight migration same name "myapp.sqlite", getting below error
terminating app due uncaught exception 'nsinternalinconsistencyexception' , reason: 'can't modify immutable model
below code
// mark: - core data stack
lazy var applicationdocumentsdirectory: nsurl = { let urls = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask) return urls[urls.count-1] }() lazy var managedobjectmodel: nsmanagedobjectmodel = { // managed object model application. property not optional. fatal error application not able find , load model. let modelurl = nsbundle.mainbundle().urlforresource("myapp", withextension: "momd")! return nsmanagedobjectmodel(contentsofurl: modelurl)! }() lazy var persistentstorecoordinator: nspersistentstorecoordinator? = { print("hello") // persistent store coordinator application. implementation creates , return coordinator, having added store application it. property optional since there legitimate error conditions cause creation of store fail. // create coordinator , store var coordinator: nspersistentstorecoordinator? = nspersistentstorecoordinator(managedobjectmodel: self.managedobjectmodel) let url = self.applicationdocumentsdirectory.urlbyappendingpathcomponent("myapp.sqlite") var err: nserror? = nil var failurereason = "there error creating or loading application's saved data." let options = [nsmigratepersistentstoresautomaticallyoption: true, nsinfermappingmodelautomaticallyoption: true] nslog("url =---- \(url)") { try coordinator!.addpersistentstorewithtype(nssqlitestoretype, configuration: nil, url: url, options: options) } catch let error nserror { print(error.localizeddescription) coordinator = nil // report error got. var dict = [string: anyobject]() dict[nslocalizeddescriptionkey] = "failed initialize application's saved data" dict[nslocalizedfailurereasonerrorkey] = failurereason dict[nsunderlyingerrorkey] = error err = nserror(domain: "your_error_domain", code: 9999, userinfo: dict) // replace code handle error appropriately. // abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. nslog("unresolved error \(error), \(error.userinfo)") abort() } return coordinator }() lazy var managedobjectcontext: nsmanagedobjectcontext? = { // returns managed object context application (which bound persistent store coordinator application.) property optional since there legitimate error conditions cause creation of context fail. let coordinator = self.persistentstorecoordinator if coordinator == nil { return nil } var managedobjectcontext = nsmanagedobjectcontext() managedobjectcontext.persistentstorecoordinator = coordinator return managedobjectcontext }()
Comments
Post a Comment