2012-08-15 64 views
2

我有一个现有的核心数据集,我想向它添加一个实体。我对添加新实体以将现有用户转换到新模型后是否可以使用轻量级迁移感到困惑。向coredata添加新实体 - 我仍然可以使用轻量级迁移吗?

当前的模态(只是显示实体):

Story 1toMany-> Sentences 

我需要:

Story 1toMany-> Sentences 1toMany-> Media 

我可以使用轻量级的迁移工具来做到这一点?

我读过的documentation

对于核心数据能够生成一个推断的映射模型, 更改必须符合一个明显的迁徙模式,例如:

简单相加的新属性删除属性A 非可选属性变为可选属性 变为非可选属性,并定义默认值重命名实体 或属性

this question似乎暗示轻量级迁移仍然可以在添加实体的情况下使用。由于新媒体实体是可选的,我不能看到它实际上会成为一个问题。

回答

3

查看wwdc 2010的核心数据视频“掌握核心数据”。他们会针对您的具体情况讨论迁移。长话短说:是的,你可以使用轻量级迁移。初始化NSPersistentStoreCoordinator实例时只需传递选项字典:

NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:@[ [NSNumber numberWithBool:YES], [NSNumber numberWithBool:YES]] forKeys:@[ NSMigratePersistentStoresAutomaticallyOption, NSInferMappingModelAutomaticallyOption]]; 
+0

MIgration从视频中的45:42开始。 – 2015-12-10 07:20:53

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]) {... 
相关问题