nsmanagedobjectmodel

Why does my NSManagedDataModel have no entities after I add another version of the DataModel?


Why does my NSManagedDataModel have no entities after I add another version of the DataModel?

MacBook Pro Late 2008, OS X 10.8.5, Xcode 5.0.2, Project has two apps, one OS X, one iOS, both share a datamodel Recently add a new dataModel Version DataModel involves four subClasses of NSManagedObject. Each of the four Classes received two identical changes, namely the addition of two attributes: modified NSDate uid NSString I did not select the option: Use scalar properties for primitive data types when generating the files of the four classes.

When the following four lines execute in the OS X app,

NSString *path = [[NSBundle mainBundle] pathForResource:@"SqliteFromXml" ofType:@"momd"];
NSURL *momdURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momdURL];

NSLog(@"path=%@\nDataModel=%@\nEntities in DM=%@",path,managedObjectModel,[managedObjectModel entities]);

This is the output:

path=/Users/appleuser/SqliteFromXml/Debug/SqliteFromXml.app/Contents/Resources/SqliteFromXml.momd

DataModel=(<NSManagedObjectModel: 0x1001d1b80>) isEditable 0, entities {
}, fetch request templates {
}

Entities in DM=( )

The momd bundle is being created on each run, but is has 0 entities.

The .app bundle Contents looks this way:

https://www.dropbox.com/s/68iy8gjqbv0m0tq/Screenshot%202014-02-16%2018.03.02.png

Many Thanks, Mark


Solution

  • Datamodel bundle name: IHM_Recipes.xcdatamodeld (developed as part of the original iOS app) There are two products both sharing a common datamodel: iHungryMePlus (iOS) SqliteFromXml (Mac)

    There was a user error in my choice of param for pathForResource: for the first line of code below:

     NSString *path = [[NSBundle mainBundle] pathForResource:@"IHM_Recipes" ofType:@"momd"];
     NSURL *momdURL = [NSURL fileURLWithPath:path];
     managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momdURL];
    

    I used pathForResourceParam == @"SqliteFromXml". Sorry to waste your time. Because a momd file named SqliteFromXml.momd was created, I wrongly concluded that pathForResourceParam was used for targeting rather than searching. I see now this was very bad logic. This approach to create a MOM came from Jeff Lamarche.