如何重构核心数据模型以使两个现有实体继承自一个新的抽象实体 [英] How to refactor a core data model to make two existing entities inherit from a new abstract entity

查看:17
本文介绍了如何重构核心数据模型以使两个现有实体继承自一个新的抽象实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此版本的 iPhone 应用程序,我正在尝试创建一个新的核心数据模型版本,在该版本中,我采用两个现有实体并将它们更改为从一个全新的抽象实体继承.我还尝试将这些实体中的几个共享属性移动到新的抽象实体上.

With this version of an iPhone app, I'm trying to create a new core data model version in which I'm taking two existing entities and changing them to inherit from a brand new abstract entity. I'm also trying to move several of the shared properties from these entities onto the new abstract entity.

当我在现有版本的应用程序上运行时,我得到一个 NSInternalInconsistencyException

When I run on an existing version of the app I get a NSInternalInconsistencyException

Cannot merge multiple root entity source tables into one destination entity root table

在此版本的应用程序之前,我已经摆脱了轻量级"迁移.这个例外是否意味着我需要创建一个映射模型?

Up until this version of the app I've gotten away with "lightweight" migration. Does this exception mean that I'll need to create a Mapping Model?

推荐答案

我已经解决了这个问题.我将在这里详细介绍,以便其他人可以将其用作资源.我不完全确定核心数据的内部工作原理,但在尝试创建一个新的抽象实体并将两个现有实体合并到其中时,它似乎窒息了.所以我想出的解决方案是抛弃旧实体并创建 3 个全新的实体,并创建一个映射模型将数据从旧实体映射到新实体.

I've solved the problem. I'm going be detailed here so other people can use this as a resource. I'm not completely sure about the inner workings of core data, but it seemed to choke when trying to create a new abstract entity and have two existing entities merge into it. So the solution I came up with was to ditch the old entities and create 3 brand new ones and create a mappingmodel to map data from the old entities onto the new ones.

给定以下需要从新抽象实体继承的实体.

Given the following entities that need to inherit from a new abstract entity.

  • 鼠标

创建一个新的模型版本 并创建 3 个新实体.删除旧的.我为新实体使用了不同的名称.我不确定是否有办法通过保持名称相同来实现这一点.

Create a new model version and create 3 new entities. Delete the old ones. I used different names for the new entities. I'm not sure if there's a way to accomplish this by keeping the names the same.

  • Serpent(代替蛇)
  • Rodent(代替鼠标)
  • Animal(其他两个将用作父级的新抽象实体)
  • Serpent (replaces snake)
  • Rodent (replaces mouse)
  • Animal (new abstract entity that the other two will use as a parent)

注意:可以在模型设计器视图中复制和粘贴属性和关系.您可以在实体之间甚至跨模型版本复制它们.只需从设计器视图中的属性列表中选择属性,然后 ⌘-C.将属性从旧实体移到新抽象实体上时,这可以节省大量时间.

Note: it's possible to copy and paste properties and relationships in the model designer view. You can copy them between entities and even across model versions. Just select properties from the property list in the designer view and ⌘-C. This is a big time saver when moving properties from the old entities onto the new abstract one.

使用 Xcode 创建映射模型.在创建对话框中,选择以前的模型版本作为源,选择新的模型版本作为目标.映射模型包含一个实体映射列表.Xcode 应该已经为旧模型版本中的每个实体自动创建了一个.它们遵循 OldEntityToNewEntity 的命名模式.尚未创建任何新实体,因此您必须添加它们:

Use Xcode to create a mapping model. In the creation dialog select the previous model version as the source and the new model version as the destination. The mapping model holds a list of entity mappings. Xcode should have automatically created one for each of the entities in your old model version. They follow the naming pattern of OldEntityToNewEntity. None of the new entities will have been created yet so you have to add them:

  • SnakeToSerpent

  • SnakeToSerpent

MouseToRodent

MouseToRodent

(不要为抽象的 Animal 实体制作一个).

(do not make one for the abstract Animal entity).

在每个新映射的属性检查器中,选择旧实体作为源,新实体作为目标.

In the property inspector for each of the new mappings select the old entity as the source and the new one as the destination.

每个实体映射中的一些属性和关系应该已经存在.在旧实体和新实体中具有相同名称的任何属性都应该被自动检测并正确设置.您必须为移动到抽象实体的任何属性添加属性映射.(与关系和获取的属性相同)只需参考您的旧模型版本,以确保您包含您计划继续使用的所有属性.

Some of the properties and relationships in each entity mapping should already be present. Any property that has the same name in both the old entity and the new one should have been automatically detected and setup correctly. You'll have to add a property mapping for any property that got moved to the abstract entity. (same with relationships and fetched properties) Just reference your old model version to make sure you included all the properties you plan to continue using.

应该是这样的.

这篇关于如何重构核心数据模型以使两个现有实体继承自一个新的抽象实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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