CoreData自定义映射模型

发布于 2024-09-06 13:55:56 字数 267 浏览 4 评论 0原文

我在将以下情况从核心数据模型的 v1 映射到 v2 时遇到问题。

在模型的 v1 中,我有一个名为 book 的实体,具有作者属性。我在那里保存了作者的名字和姓氏,甚至保存了几位作者的名字和姓氏。我知道设计很糟糕,但事实就是如此。

在模型的 v2 中,我对其进行了改进,并添加了具有名字和姓氏属性以及与书籍的关系的实体作者。有谁知道我如何以这样的方式自定义映射模型,即它调用一个函数(返回作者数量以及分隔的名字和姓氏)并创建与该函数的输出相关的新实体?

谢谢 b00tsy

I have a problem mapping the following situation from v1 to v2 of a core data model.

In v1 of the model I had an entity named book with an attribute author. There I saved the first and last name of the author and even first and last names of several authors. Very poor design I know, but that's how it was.

In v2 of the model I made it better and added the entity author with the attributes firstname and lastname and a relationship to book. Does anyone know out there how I can customize the mapping model in such a manner, that it calls a function (which returns the number of authors and the first and last name separated) and creates the new entities regarding to the output of that function?

Thanks
b00tsy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-09-13 13:55:56

首先在源模型和目标模型之间创建映射模型。从那里,您需要在映射模型中选择要处理此分割的对象。在映射模型编辑器的右侧,您将看到可以在其中命名 NSEntityMigrationPolicy 类的位置。通过在此处添加自定义映射策略类,您可以告诉迁移使用您的代码而不是标准代码。

从那里创建该类并使其成为 NSEntityMigrationPolicy 的子类。在该类内部重写方法 -createDestinationInstancesForSourceInstance:entityMapping: manager: error:。每个对象都会调用此方法一次,您有责任创建目标对象并将所有属性从源复制到目标。作为该副本的一部分,您可以按照您认为合适的逻辑将名称分为名字和姓氏。在该方法结束时,请确保调用 -associateSourceInstance: withDestinationInstance: forEntityMapping: 以便 NSMigrationManager 知道新创建的目标对象,并且迁移的其余部分将正常工作。

注意:您不需要重写任何与关系相关的方法,除非您还需要为这些方法编写自定义代码。

这就是全部内容了。

You start off by creating a mapping model between the source and destination models. From there you want to select the object in the mapping model that you are going to want to handle this split. On the right side of the mapping model editor you will see where you can name a class that is the NSEntityMigrationPolicy. By adding a custom mapping policy class here you can tell the migration to use your code instead of the standard code.

From there, create the class and have it subclass NSEntityMigrationPolicy. Inside of that class override the method -createDestinationInstancesForSourceInstance: entityMapping: manager: error:. This method will be called once per object and it is your responsibility to create the destination object and copy all of the attributes from the source to the destination. As part of that copy you can split the name into first name and last name by however logic you feel is appropriate. At the end of that method make sure you call -associateSourceInstance: withDestinationInstance: forEntityMapping: so that the NSMigrationManager is aware of the newly created destination object and the rest of the migration will work correctly.

NOTE: you do not need to override any of the relationship related methods unless you need to write custom code for those as well.

That is all there is to it.

明明#如月 2024-09-13 13:55:56

核心数据模型版本控制和数据迁移编程指南 向您展示如何进行迁移部分。

至于名字/姓氏的分离,你可能会遇到问题。在哪里区分名字和姓氏?通过空格?那“德尔·托雷斯”之类的呢?使用“中间名”的作者又如何呢?

这是一项艰巨的任务,也是您应该考虑的一项任务。

The Core Data Model Versioning and Data Migration Programming Guide shows you how to do the migrating part.

As for the separation of first/last name, you might have a problem there. Where do you separate first/last name? By spaces? What about "Del Torres" and the like? What about authors who use their "middle names"?

That's a tough one and one you should consider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文