Monday, 13 August 2012

Lightweight Database Migration in CoreData

Lightweight Migration:

This is an automatic feature of Core Data, but is limited to only simple migrations.
You can use this technique if the changes are among the following:
1. Adding a new attribute to an entity.
2. Renaming of an entity or an attribute.
3. Changing an attribute to optional or non-optional, whether it requires a value on creation.

Schema Changes, Versioning and Migrations
When you are developing an application you will almost never get the schema right the first time. So, Core Data has support for schema versioning and data migration. This allows you to define a new version of the Core Data schema by adding a new attribute or entity to the store. Then define how the store has to migrate from one change to another.

Creating a new xcdatamodel (Xcode 4.3.2)

If you want to do a lightweight migration first step is to create a new xcdatamodel for new schema.

1. Click on your xcdatamodeld file present in bundle.
2. Click on Editor Menu and select Add Model Version.

4. Here specify the version name and Base Model and click Finish.
5. And you will find the newly created xcdatamodel file under the xcdatamodeld group.
6. Now click on xcdatamodeld in bundle and open File Inspector, under Versioned Core Data Model select the newly created xcdatamodel file.

8. A green Tick will appear to left of the currently selected xcdatamodel file.

10. And now you are done with data migration, now Core Data will automatically handle the new schema for the database.

11. Set the Persistent Store options for automatic migration in AppDelegate

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"YOURDB.sqlite"];
//Enabling automatic migrations in persistent store coordinator
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
return __persistentStoreCoordinator;

No comments:

Post a Comment