开发者

Must I enable versioning in my first release in order to use lightweight migration in subsequent releases?

My fear is the change of the data model in subsequent releases.

I created a new xcdatamodel file in Xcode which is not v开发者_如何学JAVAersioned by default. I know you can click somewhere and make it "the first version". In the Groups and Files tree the xcdatamodel file gets a thick black arrow on the left side which you can click to see all the versions inside.

My file does not have that thick arrow so is not versioned.

Does this cause big problems later? Is it needed to version it right from the start to make lightweight migration work later? Once the app is shipped to users this can't be changed anymore.


You do not need to set up lightweight migration in your first release. When you do decide to migrate, you need to do two things. First, you must keep a copy of each version of your data model. These models are usually held in a .xcdatamodeld file. Alternatively, you can just keep a bunch of .xcdatamodel files. However, it's best to use the .xcdatamodeld file to keep everything organized. Second, you must activate lightweight migration with your persistent store coordinator. When creating your persistent store coordinator, you will do something like the following:

__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

// Automatically migrates the model when there are small changes.
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
                          nil];
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                           configuration:nil 
                                                     URL:storeURL 
                                                 options:options 
                                                   error:&error];

Remember that lightweight migration can only do so much. If you need to make heavier changes, then you will need to create a mapping model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜