Today I was playing with Core Data technology and I was trying to check whether it is possible to update the store schema according to the changes I made to the entities, relationships and whatever else.
I found out that the important steps are:
- Use the versioning: before modifying your current schema you should select “Editor -> Add Model Version..”
- Edit as much as you like
- Select the new version as the active one
- Go to your app delegate file and modify the
- (NSPersistentStoreCoordinator *) persistentStoreCoordinatormethod as follows:NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"YourApp.storedata"]; __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error]) { // The rest of the code here } return __persistentStoreCoordinator;
The highlighted lines are the one to be added. Please notice that row 8 needs to be updated withoptions:options. I only tried it usingNSSQLiteStoreTypeas persistent store type but it should work fine even withNSXMLStoreType.
References: