Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 444
Sean Rome Avatar asked Jul 08 '10 16:07

Sean Rome


2 Answers

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! :)

like image 53
benvolioT Avatar answered Sep 20 '22 21:09

benvolioT


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.

like image 45
Marcus S. Zarra Avatar answered Sep 19 '22 21:09

Marcus S. Zarra