Core Data Migration and Versioning

Yep, another Core Data post. Hat tip to Jeff LaMarche for the solution on this one.

After adding a new data model version, then moving some furniture around and adding a few new bits and bobs, I hit Build & Go and was hit with the fantastically detailed error:

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

Head on over to Jeff’s post for the full explanation of what’s going on here but to cut a long story short, there’s an issue with how the .momd bundle works in tandem with Apple’s template code.

Replace the standard -managedObjectModel method with the following, and as long as all other prerequisite pieces are in order you should be fine.

- (NSManagedObjectModel *)managedObjectModel {
    
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YourAppName" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
       
    return managedObjectModel;
}