Migration :
Every persistent store is bind to particular version of data model by keeping reference of data model identifier. If data model changes, we need to tell Core Data how to migrate the date of persistent store to new data model version.
There are two type of migration :
1. Light Weight Migration: It Uses CoreData ability to infer model schemas change automatically and can be easily used when add or remove entities, attributes and modify relationship between them. Apple recommends to take advantage of light weight migration whenever possible.
2. Heavy Weight Migration: It require complex process and require developer to manage different mapping files and write complicated , verbose policy to manually shuttle data different data model.
Mapping Model : It define how one version of data model is related to other version.
Why We Need CoreData Migration:
Whenever we edit schema of our Application data model we need to tell CoreData about change in data model of persistent store coordinator. if user doesn't perform data migration then application will crash and giving
fatal error -- "The model used to open the store is incompatible with the one used to create the store";
Perform Data Migration
1. Add Version in data model
Select DataModel and then go to Editor and select Add Model Version like in below screen
Enter Version name as shown in below screen and click on Finish button :
2. In Project Navigator we can see different data model along with current data model selected.
Green checkmark represent active data model.
3. Perform Migration :
For Implementing Light weight migration we need to pass two keys in option parameter of addPresistentStore( ofType : configurationName : at : options : ) method in our CoreData Manager class
This methods Accept dictionary as last parameter so for supporting light weight we will pass two dictionary of option with two keys as :
a. NSInferMappingModelAutomaticallyOption : Pass TRUE — CoreData attempts
to infer mapping model for migration based on data model version.
b. NSMigratePresistentStoresAutomaticallyOption: Pass TRUE — Will Indicate CoreData to automatically perform migration if there is any change in data model.
Complete Code would look like as :
do {
let options = [ NSInferMappingModelAutomaticallyOption : true,
NSMigratePersistentStoresAutomaticallyOption : true]
try coordinator!.addPersistentStore(ofType: NSXMLStoreType, configurationName: nil, at: url, options: options)
} catch {
// Replace this implementation with code to handle the error appropriately.
/*
Typical reasons for an error here include:
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
failError = error as NSError
}
‘
Code highlighted in the below image:
Now application will run successfully without any fatal error that occur when we migrate data from one data model version to other.
We can now easily Add , remove new entity , attributes and define relation between them.



No comments:
Post a Comment