Core Data: Error, "Can't Merge Models With Two Different Entities Named 'foo' "
Asked Answered
H

9

30

I'm working on an iPhone app that uses Core Data. Most times, I just test in the simulator, but occasionally pump the app down to the iPad to make sure.

I've recently changed my Core Data model, and now when I send the app to the iPad, I get a SIGABRT exception telling me:

 Can't merge models with two different entities named 'foo'

OK, that I understand. Old version of the database exists on the device. So, I (try to) kill the old version by press/holding the application's icon until it starts wiggling, and then tap its "X". The iPad asks me if I want to delete the application and all of its data. I say yes.

I rebuild the app, targetting the iPad, and get the same error.

Is there a trick to getting the old database to really go away?

Hickie answered 8/7, 2010 at 16:21 Comment(0)
M
61

For those who come across this question after trying to use core data lightweight migrations:

I was having this issue even after following the instructions for creating a new version of my data model. I noticed that there were two ".mom" files in my application bundle, one ".mom" and one ".momd" directory that contained ".mom" files.

The key is to replace the implementation of - (NSManagedObjectModel *)managedObjectModel that is generated for you with this implementation:

- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Foo" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    return managedObjectModel; }

where 'Foo' is the name of your data model.

Hopefully this is useful to someone - I spent WAY too many hours beating my head against the wall on this. Thanks again, Apple! :)

Maiga answered 17/11, 2010 at 3:22 Comment(2)
Does anybody have a solution that doesn't involve deleting the app and reinstalling? None of these current solutions would fix an existing install.Amylaceous
But where is the implementation of - (NSManagedObjectModel *)managedObjectModel I can't find it in my projectGravely
R
23

The persistent store will remain until you delete the app off of your device just like in the simulator. If you really want to start over, then delete the app off of your iPad and it will use the new model.

However as everyone else has pointed out, that is not the error you are getting, Do a clean build of your application (meaning select Build -> Clean from the menu in Xcode) and do a fresh build. If the error still remains then you have more than one xcdatamodel file being compiled in your project.

Roue answered 8/7, 2010 at 20:22 Comment(3)
By utter coincidence, I hit this exact problem not four hours after reading this answer. A clean didn't resolve the problem but deleting the app from the simulator following a clean did. Weird.Agamic
The Sim has the same issue as the build dir. When you deploy a new build it won't delete files that are no longer referenced in the new app. Changes like this require deleting the app from the Sim as well. Join me in a radar on that one?Roue
This seems to be the right answer. I had indeed two xcdatamodels compiled. I assume that benvolioT's answer is a workaround without fixing the underlying issue.Swordtail
A
8

Your error message doesn't mean you have two databases i.e. persistent stores. It means you have two overlapping model files (which are source files) in the Xcode project itself. Deleting the build does not good because the project just recreates the error upon the next build.

Model files cannot overlap. You need to remove the duplication. The duplication comes from either (1) having two versions of the same file or (2) having two model files with the same entity in both.

For (1), you need to remove the older duplicate. The model files have an extension of modelName.xcdatamodel. To find the duplicate, open the target and check under Compile Sources. All the model files included in the target will be listed there. Remove the duplicate.

For (2), you can have as many model files as you like but you can't have two model files with the same entity in both. Sometimes, Core Data novices try to link to model files together by having them share one or more entities. If you've done that, you will need to remove the duplicate entity from one of the models.

Agamic answered 8/7, 2010 at 17:18 Comment(2)
He probably either renamed the model or set up a version, both of which will leave an old compiled .mom file in the build directory. That is the #1 cause of this error I have seen.Roue
That's good to know. I obsessively clean my builds so I probably seldom encounter that particular problem.Agamic
C
6

Delete the app from your iPad, then restart it. After the iPad has rebooted, do a clean build.

Cardiogram answered 31/12, 2011 at 1:31 Comment(1)
Wow, that was it! I looked really long and hard at the file system in the Simulator directory and there was nothing regarding CoreData, but it wouldn't work. Still, deleting helped, for whatever reason. Should have tried it earlier.Abdication
J
3

I had the similar problem after renaming database model. After cleaning, rebuilding and reinstalling app -- the same consequences: two different named .momd bundles with the same content appears in the application bundle (oldname.momd and newname.momd).
I solved this problem by changing "Tools version" from 4.1 to 3.2 in "Core Data Model" (File inspector of Data Model) panel. After this I completely cleared up and re-builded the project, then changed back this property to 4.1 and again re-cleared and re-builded the app.
In this case Xcode makes two files "layout" and "elements" (for older version compatibility) against "contents" in *.xcdatamodeld bundle, this cause that Xcode's data model compiler rechecks name of the model and there all will be ok with .momd bundles in the app's sandbox -- only one appears after reinstalling the app.

Junko answered 26/6, 2012 at 10:48 Comment(0)
H
2

"Can't merge models with two different entities named 'foo'"

This sounds more like two datamodels beeing merged. Try a clean rebuild of your app. Check if there really is only one datamodel in your project.

The default core data stack loads all data models in your bundle

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];   

If old models are present all of them are merged.

Husain answered 8/7, 2010 at 16:46 Comment(2)
When switching between models in an app, if we have one old model Model1 in the live app and we want to update the app and use just another new model called Model2, it's enough if we delete Model1 from project? Or Model1 will not be deleted from live app unless we specifically do it when it's updated?Jamie
what you probably want is model versioning developer.apple.com/library/mac/documentation/cocoa/conceptual/…Husain
S
1

for me only the "Reset content and settings" of the simulator worked. otherwise I could not find any problems with my project.

Separates answered 2/7, 2013 at 8:7 Comment(0)
G
0

You're eventually going to want to read about model migration so you can automatically update the core data database on the device when users upgrade.

But if you're in dev and you don't have any installed users, you can wipe out the core data database by going into iTunes with your device connected, deleting the app from your device, and syncing. Then drag the new version of your app into iTunes and re-sync. You should have a clean model.

Gadid answered 8/7, 2010 at 16:38 Comment(0)
R
0

I had the same problem.
Try to clean Shift + Cmnd + K and cleaning build folder ALT + Shift + Cmnd + K in Xcode. This worked for me fine.

Rawalpindi answered 18/8, 2016 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.