Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

iphone - How to refactor a core data model to make two existing entities inherit from a new abstract entity

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...