Reputation: 1094
I was referring a wonderful tutorial Swift Core Data Tutorial for migrations in core data.
The tut stated that migrations in core data go hand in hand with multiple versions of data model.
I was experimenting on light-weight migrations and got to figure out the following points:
Scenario - I had a data model and I tried to add a some new attributes to an entity and I got an error as -
iOS Version - 9.1 Swift Version - 2.2
"The model used to open the store is incompatible with the one used to create the store”
I then tried to enable migration without creating any new version by adding the code -
let options = [ NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true ]
try persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: URLPersistentStore, options: options)
Then I tried altering the database similarly, i.e added an attribute to an entity and hurrah !!, it didn't result in a crash. Even I tried to access the particular entity as well as attribute but did it successfully.
What happened is migration without having multiple versions.
Question - So Is migration possible without having multiple versions of data model?
Link to sample code - https://www.dropbox.com/s/mnb2dzxn56ghuet/CoreDataSwift-NSFetchedResultsController-master-2.zip?dl=0
Upvotes: 1
Views: 240
Reputation: 70936
Beginning with iOS 9, Core Data will copy the data model to the persistent store, and use that as the starting point if lightweight migration is necessary and requested. As a result, lightweight migration is possible even if the old model version is not available. This only works with SQLite persistent stores, and only for lightweight model migrations.
This was described in the What's New in Core Data session at WWDC 2015.
Upvotes: 1