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

自作多情 提交于 2019-12-06 09:08:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!