objective c - NSPersistentStoreCoordinator has no persistent stores(Schema mismatch or migration failure) core data in ios iOS -
i have added new version of data model. added new fields in new version.also set current model version newly created version.
code update:
- (nsmanagedobjectmodel *)managedobjectmodel { if (_managedobjectmodel != nil) { return _managedobjectmodel; } nsurl *modelurl = [[nsbundle mainbundle] urlforresource:@"ppusfamodel" withextension:@"momd"]; //i earlier ppusfamodel 16, added new model version ppusfamodel 17. _managedobjectmodel = [[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl]; return _managedobjectmodel; } - (nspersistentstorecoordinator *)persistentstorecoordinator { if (_persistentstorecoordinator != nil) { return _persistentstorecoordinator; } nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:sqlitename]; nserror *error = nil; _persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]]; nsdictionary *options = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithbool:yes], nsmigratepersistentstoresautomaticallyoption, [nsnumber numberwithbool:yes], nsinfermappingmodelautomaticallyoption, nil]; if (![_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:options error:&error]) { //error } return _persistentstorecoordinator; }
it worked until attributes added after point getting crash @ following code
- (void)savecontext { __block nserror *error = nil; nsmanagedobjectcontext *managedobjectcontext = self.managedobjectcontext; [managedobjectcontext performblockandwait:^{ if (managedobjectcontext != nil && _persistentstorecoordinator != nil) { if ([managedobjectcontext haschanges] && ![managedobjectcontext save:&error]) { // replace implementation code handle error appropriately. // abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. // dlog(@"unresolved error %@, %@", error, [error userinfo]); //abort(); } } }]; }
this crashed screenshot:
when install removing earlier app device works, getting crash when app update on existing version.
//if added field in old model version accidentally instead of new version how fix that?
any appreciated.
the migration failing due model changes. core data can automatically migrate –
- simple addition of new attribute
- removal of attribute
- a non-optional attribute becoming optional
- an optional attribute becoming non-optional , defining default value
- renaming entity or property
to determine if core data can perform automatic migration use nsmappingmodel’s inferredmappingmodelforsourcemodel:destinationmodel:error:
method. return nil if core data cannot complete migration.
when core data cannot perform automatic migrations, must use migration manager. apple's documentation provides sample code explain how use migration manager.
Comments
Post a Comment