Geeks With Blogs

Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C
So I have an app called TimeTag that uses Core Data and I needed to migrate to a new core data model version. 

It uses a UIManagedDoc to manage the core data file (rather than having everything dumped in the AppDelegate). I did the usual first steps:

  1. I selected my ModelName.xcdatamodelId in my project list and went up to "Editor" and selected "Add Model Version"
  2. I had it inherit from my current model version
  3. Then I selected the ModelName.xcdatamodelId file in the project listing again, and went over to the right editor pane, and selected the "File Inspector" tab. 
  4. Here, you want to make sure that Model Version has the new version you just created in the dropdown.
All was business as usual. I made the changes in my model. In this case, I added a new abstract entity that everything was inheriting from, since I'm going to be using that for my syncing logic.

I ran the project, and got the dreaded error that the database file could not be opened. Since I was using a UIManagedDoc custom class, the error was actually masked. So I set a debug point, and came across the error: 

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

I was confused, because I had this code in my loading of my model:

NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES

NSInferMappingModelAutomaticallyOption: @YES};

self.timeTagDB.persistentStoreOptions = options;

        

(TimeTagDB is my custom UIManagedDocument class). 

After googling around and looking at some Stackoverflow answers, it turns out all I had to do was creating a mapping model and add it to the project. 

1. To do that, simply Add a new file (Cmd+N) and select "Core Data" in the side pane, and finally select "Mapping Model":

2. Hit next, and now select the Data Model you are coming from.

3. Hit next again, and finally, Select the data model you are going to.

4. Name the file. I chose something like "Model24ToModel25".


Xcode should now have created a new .xcmappingmodel file in your project, and will have automatically migrated properties that kept the same name. It won't add a class for your new abstract class and that's okay. You don't need to worry about that.

Make any changes you might have to. In my case, I didn't need to do anything, but you might need to tweak some stuff, like if your abstract class will now take over certain properties.

Run the project now. It should be good to go! (At least it was in my case).

Thanks to Christian for his SO post!


Posted on Friday, August 9, 2013 3:15 PM ios development , core data , uimanageddocment | Back to top


Comments on this post: Fixing: Cannot merge multiple root entity source tables into one destination entity root table

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © samerpaul | Powered by: GeeksWithBlogs.net