CoreData Migration (Versioning of CoreData)

18 / Mar / 2015 by Ashu Baweja 1 comments

What is Migration ?

Migration is a way of telling Xcode how to transition the data from the old model to the new model .

Why migration required ?

When the model does not match the store, a migration is required. In order to perform a migration, Core Data (technically, an instance of NSMigrationManager) requires these things:

  • The destination managed object model (the one with the changes)
  • A managed object model that can open the existing store
  • The ability to infer mapping between the two models (for a lightweight migration), or a manual mapping model
  • Permission to attempt to perform an automatic migration (for a lightweight migration)

It is therefore absolutely essential that you never make changes to the managed object model for a released version of an app. That is, if your app is already in the App Store, don’t change a single thing in that version of the managed object model.

Solution for the Crash while update an application from the app store:

Create a new version of the managed object model! This reminds me to mention some other best practices to adopt when working with Core Data:

  • Create a new model version for every release version of an app
  • Keep a copy of every release version of an app (you’re already doing this, right?)
  • Keep a copy of every release version backing SQLite store containing suitable test data

Migrations can handle the following changes:

  • Adding or removing an entity, attribute, or relationship
  • Making an attribute non-optional with a default value
  • Making a non-optional attribute optional
  • Renaming an entity or attribute using a renaming identifier

Getting Started:

  • Create a new version of the managed object model – select .xcdatamodeld file in Xcode and then from the menu select Editor\Add Model Version

  • Screen Shot 2015-03-15 at 9.10.59 PM

  • Set version name and select model based on which you want to create new model.
  • Switch to latest version of model – select .xcdatamodeld file from Project Navigator and go to FILe Inspector and select latest core data model

  • Screen Shot 2015-03-15 at 9.11.54 PM

  • Now you will see a little green circle with white checkmark badge on latest .xcdatamodel in the Project Navigator.

  • Screen Shot 2015-03-15 at 9.12.01 PM

  • Add permissions to attempt to perform an automatic migration

Open .m file containing core data related code and add the following code immediately after
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

NSDictionary *options = @{ 
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES 
};


Change the next line to pass the options dictionary you just created to
addPersistentStoreWithType:configuration:URL:options:error:

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
         configuration:nil
         URL:storeURL options:options error:&error]) {


  • Now make changes to an core data entity, attribute, or relationship whatever you want
  • Delete CoreData NSManagedObect Subclass for entities in which you have made changes and create new file (New File/ CoreData/ NSManagedObect)

  • Screen Shot 2015-03-15 at 9.14.32 PM

  • Select latest Data Model and click Next

  • Screen Shot 2015-03-15 at 9.15.30 PM

  • Select entities in which you have made changes and click next


Screen Shot 2015-03-15 at 9.15.39 PM

Run your app and check out its working fine. Congratulations! You just successfully performed a Core Data migration.

FOUND THIS USEFUL? SHARE IT

comments (1 “CoreData Migration (Versioning of CoreData)”)

Leave a comment -