iphone - Development
Core Data Migration This is the definitive guide on core data migration. Core data has some big benefits but it has a sinlge big gotcha that gives people a lot of headaches: If you change the schema at all, your app will crash. The solution is Core Data Migration, but if you go to the apple docs or any blog on the net, you will get quickly confused. Until now. These simple steps will fix your problems.This first thing to know about core data migration is avoid all Apple documentation. It will only confuse you.
This guide will work for just about all migration needs, light, heavy, big, small you name it. Step 1:
Click on your .xcdatamodel file (the one shows your database schema in visual form)
Click "Design" --> "Data Model" --> "Add Model Version"
Step 2:
Make changes to the new model
Compile and run your app to verify that it still works (using the old model)
Step 3:
Click on your .xcdatamodel file (New one)
Click "Design" --> "Data Model" --> "Set Current Version"
Step 4:
click on your main AppDelegate.m file.
search for "addPersistentStoreWithType"
replace that code with this code:
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
[persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error];
This it! Compile your code and it should work fine using the new schema.
|