the*_*end 5 cocoa core-data core-data-migration mapping-model
我有Core Data应用程序,我一直在迁移(升级)核心数据模型.每次创建新版本时,我都会为每个版本创建一个映射模型.现在我有16个版本,我有这样的映射模型:1to2.xcmappingmodel 2to3.xcmappingmodel 3to4.xcmappingmodel ...等.最多16
这样可以正常工作,但是当一个用户拥有版本10的数据文件并更新具有版本16的应用程序时会出现问题.有些我认为Core Data会自动从10升级到16,但是出现错误显示"Missing"映射模型".为了确保映射模型在正确的地方,我将它逐个升级到每个版本(10到11,11到12等等),它确实有用......这是我的代码.
我用这段代码指定模型版本:
NSBundle *modelWrapper = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"TaskApp_DataModel" ofType:@"momd"]];
NSString *modelPath = [modelWrapper pathForResource:@"TaskApp_DataModel 16" ofType:@"mom"];
NSLog(@"%@",modelPath);
managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
Run Code Online (Sandbox Code Playgroud)
我在这里定义了Migrate Automatic选项:
NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"storedata-sql"]];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:dict
error:&error]){
Run Code Online (Sandbox Code Playgroud)
有谁知道如何升级这个?谢谢.
小智 1
它只会尝试从用户现有版本(可能是 v1)转到当前版本。如果您有 3 个版本,那么您需要 v1-v2、v2-v3、v1-v3 的映射。16个版本都发货了吗?如果是这样,您可能需要开始制作新的迁移地图,如果您以前没有尝试过,那么启用自动迁移也可能是值得的,因为它可以很好地填补空白。我觉得是这样的:
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSInfersMappingModel];
但你必须仔细检查这一点。
很抱歉带来坏消息
| 归档时间: |
|
| 查看次数: |
1054 次 |
| 最近记录: |