0

I am new in iOS development.I try to update a existing core data model in my app. I want to a add a new entity in core data model but when I change anything in my core data model the app crashes . I am not understand how these entities are linked with my existing sqlite database tables and columns . Even when I change the name of entity or attributes the app crashes and give me this error - 'The model used to open the store is incompatible with the one used to create the store.'

I always reinstall the app in simulator after any change in core database but not work .Can anyone suggest me how core data is linked with sqlite table and columns and how i migrate my Core data model with add some more entities and attributes in it.

Thanks for any suggestion!

2
  • Did you search for Core Data migration ? Commented Mar 8, 2014 at 11:44
  • Make sure you have deleted any existing stores before running the app. Commented Mar 8, 2014 at 20:28

3 Answers 3

0

Apple does not document how core data entities are persisted in sqlite. Do not touch the data on sqlite level unless you know exactly what you are doing.

If you need to change the model then to that in xcode properly.

Especially: If you have earlier versions of the app in the store or distributed through other channels then consider a migration strategy. Core data is quite supportive to that. For that you would keep a copy of the original version of the model and use xcode to create a new version of that model. If the changes are just about additional entities or additional fields then the automatic migration should do.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are still in development process, you can uninstall the app from the simulator (as on your iDevice : long press app icon, then press the X)

You already publish your app : as proposed by @wain, check for good tutorial on Core Data migration

Comments

0

This is a migration.

You will need to create a new version of your Core Data Model (this is done in Xcode) and then set the new model as the current version. From there you add the entity to your new version. Further, you need to turn on "automatic" migration in Core Data so that Core Data will migrate your existing data from the previous version to the new version.

You can do this by altering the NSPersistentStoreCoordinator initialization code and adding the options:

NSMutableDictionary *options = [NSMutableDictionary dictionary];
options[NSMigratePersistentStoresAutomaticallyOption] = @YES;
options[NSInferMappingModelAutomaticallyOption] = @YES;
options[NSSQLitePragmasOption] = @{ @"journal_mode":@"DELETE" };

NSError *error = nil;
NSPersistentStore *store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]);

if (store == nil) {
  NSLog(@"Error initializing PSC: %@\n%@", [error localizedDescription], [error userInfo]);
}

If your store fails to migrate (unlikely) automatically then you will need to perform a heavy or manual migration. You can search on StackOverflow for an infinite variety of discussions on that topic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.