Sunday, December 7, 2014

CoreData data model migration

This morning I came across the issue when i needed to modify slightly the data model in the iPhone application, but when i tried to run the app in the emulator the app bombed out with the following error: "The model used to open the store is incompatible with the one used to create the store".

So the core data recognised that model has changed and failed to create managed context.

A quick search in Google brought me to this article that explains how to deal with such issues when the changes to the mode qualify for so called "light migration".


I take a liberty to copy the solution here:

  1. Open your .xcdatamodeld file
  2. click on Editor
  3. select Add model version...
  4. Add a new version of your model (the new group of datamodels added)
  5. select the main file, open file inspector (right-hand panel)
  6. and under Versioned core data model select your new version of data model for current data model
  7. THAT'S NOT ALL!! You should perform so called "light migration".
  8. Go to your AppDelegate and find where the persistentStoreCoordinator is being created
  9. Find this line if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
  10. Replace nil options with @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} (actually provided in the commented code in that method)
  11. Here you go, have fun!

No comments:

Post a Comment