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

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

问题描述

使用这个版本的iPhone应用程式,我试图建立一个新的核心数据模型版本,其中我将两个现有的实体,并改变它们继承一个全新的抽象实体。我也试图将这些实体中的几个共享属性移动到新的抽象实体上。



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

 无法将多个根实体源表合并到一个目标实体根表

直到这个版本的应用程序,我已经得到了轻量级的迁移。这个异常意味着我需要创建一个映射模型?

解决方案

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



给定以下实体需要从一个新的抽象实体继承。




  • Snake

  • 鼠标



步骤1 - 创建新模型版本



创建新模型版本并创建3个新实体。删除旧的。我为新实体使用了不同的名称。我不知道是否有一个方法来实现这一点,保持名称相同。




  • (取代蛇)

  • Rodent (替换鼠标)

  • Animal 其他两个将用作父级)



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



第2步 - 创建映射模型



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




  • SnakeToSerpent

  • MouseToRodent



    (不要为抽象动物实体创建一个)。




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



步骤3 - 映射属性



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



这应该是。 p>

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.

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?

解决方案

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.

  • Snake
  • Mouse

Step 1 - create new model version

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 (replaces snake)
  • Rodent (replaces mouse)
  • Animal (new abstract entity that the other two will use as a parent)

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.

Step 2 - Create a Mapping Model

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
  • MouseToRodent

    (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.

Step 3 - map the properties

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.

That should be it.

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

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