Swift - Coredata Migration - 根据旧属性值设置新属性值 [英] Swift - Coredata Migration - Set new attribute value according to old attribute value

查看:102
本文介绍了Swift - Coredata Migration - 根据旧属性值设置新属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的核心数据实体之一 - Entity1 - 有一个名为 isSaved Boolean 属性。

One of my current core data entities - Entity1 - has a Boolean attribute called isSaved.

在新的核心数据模型中,我打算删除 isSaved 属性并添加一个新的 Int 属性名为 type 。对于所有保存的Entity1对象,我想根据 isSaved 的值设置的值旧核心数据模型。 (例如,如果isSaved为true,则type为1,else类型为2)。

In the new core data model, I am planning to remove isSaved attribute and add a new Int attribute called type. And for all saved Entity1 objects, I'd like to set the value of type according to the value of isSaved in old core data model. (e.g. if isSaved is true, then type is 1, else type is 2).

我读过一些关于轻量级核心数据迁移的文章,但没有一篇似乎很有帮助。

I've read some articles about light weight core data migration, but none of them seems helpful.

只是想知道是否有任何方法可以使我计划的迁移工作?

Just wondering if there is any way that can make my planned migration work?

推荐答案

轻量级迁移无法做到这一点。您必须创建一个映射模型和 NSEntityMigrationPolicy 的子类。对于大多数iOS开发人员而言,这并不困难,但这是一个陌生的领域。步骤如下:

Lightweight migration can't do this. You'll have to create a mapping model and a subclass of NSEntityMigrationPolicy. It's not difficult but it's unfamiliar territory for most iOS developers. The steps run like this:


  1. 创建映射模型。在Xcode中,文件 - >新建 - >映射模型。当您单击下一步时,Xcode将询问此映射的源(旧)和目标(新)模型文件。

  2. 模型文件将尽可能推断映射。其他一切都是空白的。使用类型和其他一些属性,它看起来如下所示。 $ source.timestamp 等条目意味着复制迁移前的现有值。

  1. Create the mapping model. In Xcode, File --> New --> Mapping Model. When you click "Next", Xcode will ask for the source (old) and destination (new) model files for this mapping.
  2. The model file will infer mappings where possible. Everything else will be blank. With your type and some other properties, it'll look something like the following. Entries like $source.timestamp mean to copy the existing value from before the migration.


  1. 创建<$ c的新子类$ C> NSEntityMigrationPolicy 。给子类一个明显的名称,如 ModelMigration1to2 。此类将告诉Core Data如何将旧布尔值映射到新的整数值。

  1. Create a new subclass of NSEntityMigrationPolicy. Give the subclass an obvious name like ModelMigration1to2. This class will tell Core Data how to map the old boolean value to the new integer value.

向子类添加方法以转换值。像下面这样的东西。方法名称无关紧要,但如果您选择描述性内容则很好。你需要在这里使用ObjC类型 - 例如 NSNumber 而不是 Int Bool

Add a method to the subclass to convert the value. Something like the following. The method name doesn't matter but it's good if you choose something descriptive. You need to use ObjC types here-- e.g. NSNumber instead of Int and Bool.

func typeFor(isSaved:NSNumber) -> NSNumber {
    if isSaved.boolValue {
        return NSNumber(integerLiteral: 1)
    } else {
        return NSNumber(integerLiteral: 2)
    }
}


  • 返回映射模型并告诉它使用您的子类作为其自定义映射策略。这是在自定义政策下右侧的检查员。请务必包含模块名称和类名。

  • Go back to the mapping model and tell it to use your subclass as its custom mapping policy. That's in the inspector on the right under "custom policy". Be sure to include the module name and class name.


    1. 告诉映射模型使用您之前创建的函数来获取类型的值 isSaved 属性的c $ c>属性。以下说用一个参数调用名为 typeForIsSaved:的自定义策略类上的函数(很重要) ,该参数应该是 $ source (旧托管对象)上的 isSaved 值。

    1. Tell the mapping model to use that function you created earlier to get values for the type property from the old isSaved property. The following says to call a function on the custom policy class named typeForIsSaved: (the : is important) with one argument, and that the argument should be the isSaved value on $source (the old managed object).

    迁移应该现在工作。您不必告诉Core Data使用映射模型 - 它会发现需要迁移并查找与旧模型和新模型版本匹配的模型。

    Migration should now work. You don't have to tell Core Data to use the mapping model-- it'll figure out that migration is needed and look for a model that matches the old and new model versions.

    一些注意事项:


    • 如果您遇到类似的错误而导致无法创建对于名为... 的类的映射策略,然后在步骤5中忘记了上面的模块名称(或者说错了)。

    • 如果你遇到崩溃的话无法识别的选择器错误,然后步骤4中的方法签名与您在步骤6中输入的内容不匹配。

    • If you crash with an error that's something like Couldn't create mapping policy for class named... then you forgot the module name above in step 5 (or got it wrong).
    • If you get a crash with an unrecognized selector error then the method signature in step 4 doesn't match what you entered in step 6.

    这篇关于Swift - Coredata Migration - 根据旧属性值设置新属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆