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
286 views
in Technique[技术] by (71.8m points)

iphone - 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?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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


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

...