开发者

Can't solve, "Can't merge models with two different entities named"

I'm working with a Core Data project in iOS 4.1 (targeting 3.1). When I add a Data Model version I get the dreaded "Can't merge models with two different entities named xxx" error." Cleaning Targets does not help. Deleting the build directory does not help. The only thing that solves the issue is deleting the previously installed version of the app and installing fresh, which defeats the entire purpose of versioning and data migration.

I've got another project that's successfully using this process and they share the same code libraries I've put together for handling Core Data. I cannot figure out what could be hanging up the one project.

I based my Core Data code on, Grouchal's answer on this link text and Jeff Lamarche's link text. In troubleshooting I've poured over these as well as other, similar articles on the net. Mo开发者_运维问答st people with this issue seem to have had good luck with "Clean All Targets." But I'm striking out.

Any suggestions?


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.

Based on that, I was able to find this excellent post explaining the issue and offering a solution.

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


Here's my solution

((RootViewController *) [self.tabBarController.viewControllers objectAtIndex:0]).managedObjectContext = self.managedObjectContext;
((AlbumViewController *) [self.tabBarController.viewControllers objectAtIndex:1]).managedObjectContext = self.managedObjectContext;
((CameraViewController *) [self.tabBarController.viewControllers objectAtIndex:2]).managedObjectContext = self.managedObjectContext;
((VideoViewController *) [self.tabBarController.viewControllers objectAtIndex:3]).managedObjectContext = self.managedObjectContext;


I discovered that a 3rd party library I was using was creating it's own CoreData data store and was using the "merge models" method or data model management. I contacted the vendor and the helpfully fixed the situation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜