Just creating a new iPhone application with CoreData. My application builds without any warnings or errors and has my .xcdatamodeld in it as well which has no errors or warnings.
For some reason my managedObjectModel won’t initialise and I can’t work out why.
- (NSManagedObjectModel *)managedObjectModel {
if (__managedObjectModel) {
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"BAK" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
The modelURL resolves correctly and points to my compiled data model but the managedObjectModel stays at nil after its alloc line. There is no output into the console either. What can cause this or what am I missing?
The __managedObjectModel is declared as follows:
.h
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
.m
@synthesize managedObjectModel = __managedObjectModel;
Thanks for your help.
My bet is you changed the model somehow, most likely the name (“BAK” to something else or something else to “BAK”).
That initialization of the NSManagedObjectModel is really simple. If it has been initialized and exists, return it, otherwise initialize the ManagedObjectModel using the name you provided (“BAK”).
The only other explanation is that somehow, someway, the Core Data model didn’t get initialized in the main bundle location, but somewhere else.