我有一个现有的核心数据集,我想向它添加一个实体。在添加新实体以将现有用户转换为新模型后,我对是否可以使用轻量级迁移感到困惑。
当前模式是(只显示实体):
Story 1toMany-> Sentences
我需要:
Story 1toMany-> Sentences 1toMany-> Media
我可以使用轻量级迁移工具来执行此操作吗?
我已阅读documentation:
为了使核心数据能够生成推断的映射模型, 更改必须符合明显的迁移模式,例如:
简单添加新属性删除属性A. 非可选属性变为可选属性 变为非可选,并定义默认值重命名实体 或财产
但是this question似乎建议轻量级迁移仍然可以添加实体。由于新媒体实体是可选的,我无法看到它实际上是一个什么问题。
答案 0 :(得分:3)
查看来自wwdc 2010的核心数据视频“掌握核心数据”。他们讨论了针对特定案例的迁移。长话短说:是的,你可以使用轻量级迁移。只需在初始化NSPersistentStoreCoordinator实例时传递选项字典:
NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:@[ [NSNumber numberWithBool:YES], [NSNumber numberWithBool:YES]] forKeys:@[ NSMigratePersistentStoresAutomaticallyOption, NSInferMappingModelAutomaticallyOption]];
答案 1 :(得分:3)
是的,您可能可以使用轻量级迁移。根据我的经验,我发现你需要在编辑菜单下添加模型版本... 之前你对CoreData模型进行了更改。这种方式有一个前后方案来映射。然后,您需要将新模型设置为当前模型。 (您现在可以将实体添加到Core Data模型。请确保您正在使用正确的模型。)
最后,您需要确保传递用于初始化PersistentStoreCoordinator的选项。
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool: YES],NSMigratePersistentStoresAutomaticallyOption,[NSNumber numberWithBool:YES],NSInferMappingModelAutomaticallyOption, nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {...