【问题标题】:Adding a new entity to coredata - can I still use lightweight migration?向 coredata 添加新实体 - 我还能使用轻量级迁移吗?
【发布时间】:2012-08-15 18:36:47
【问题描述】:

我有一个现有的核心数据集,我想向其中添加一个实体。在添加新实体将现有用户转换到新模型后,我是否可以使用轻量级迁移有点困惑。

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

Story 1toMany-> Sentences

我需要:

Story 1toMany-> Sentences 1toMany-> Media

我可以使用轻量级迁移工具来执行此操作吗?

我在documentation看过:

为了让 Core Data 能够生成推断映射模型, 更改必须符合明显的迁移模式,例如:

简单添加一个新属性 删除一个属性 A 非可选属性变为可选 可选属性 变为非可选,并定义默认值重命名实体 或财产

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

【问题讨论】:

    标签: ios cocoa-touch core-data


    【解决方案1】:

    是的,您可能能够使用轻量级迁移。根据我的经验,我发现您需要在编辑器菜单下添加模型版本...您对 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]) {...
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 视频中的迁移从 45:42 开始。
      猜你喜欢
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多