views:

112

answers:

6

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 ?

Thanks !

A: 

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.

chrispix
A: 

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

Martin Brugger
+2  A: 

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.

TechZen
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.
Marcus S. Zarra
That's good to know. I obsessively clean my builds so I probably seldom encounter that particular problem.
TechZen
+1  A: 

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.

Marcus S. Zarra
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.
TechZen
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?
Marcus S. Zarra
A: 

Clean build did the trick. Swear to God I did that !

Apparently not.

Works fine now that I've gotten your generous help and a little sleep and coffee.

Thanks all !

Kaplah !

Sean Rome
You should hit the checkmark next to Marcus S. Zarra's answer so that the (1) he gets the rep, (2) the system shows the question as answered for the benefit of future readers and (3) you get the boost on your answer rate.
TechZen
A: 

I just ran into this problem while testing on a device. Drove me nuts until I removed the app completely and did a reinstall.

In my case the error was because I also renamed the xcdatamodel file. It seems XCode doesn't remove files on the device that are not in the bundle, so as a result my old and new moms were hanging around causing havoc.

freespace