My app uses CoreData, with a model named : mupo-ios.xcdatamodel. This model is the second version of the original Model : Model.xcdatamodeld. I was using the managedObjectContext through a document:
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"MyDocument"];
self.document = [[UIManagedDocument alloc] initWithFileURL:url];
// Set our document up for automatic migrations
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
self.document.persistentStoreOptions = options;
}
And later, I access the managedObjectContext :
self.managedObjectContext = self.document.managedObjectContext;
Now, I want to access to the main context directly, here's the code:
- (NSManagedObjectContext *)mainContext {
if (!_mainContext) {
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_mainContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
}
return _mainContext;
}
The problem is when I try to specify the managedObjectModel :
- (NSManagedObjectModel *)managedObjectModel {
if (!_managedObjectModel) {
NSString *localModelPath = [[NSBundle mainBundle] pathForResource:@"mupo-ios" ofType:@"momd"];
NSURL *modelUrl = [NSURL fileURLWithPath:localModelPath];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl];
}
return _managedObjectModel;
}
The momd's file can't be found so the managedObjectModel can't be initialized. Can anybody helps me?
This problem happened to me in a complex scenario. I had two xcodeproj in a single workspace.
I tried to use the same xcdatamodeld file in the second project by dragging it from the first project to the second inside xcode. This created a reference correctly, but even when adding the file to the "compile resource", the modm file wasn't created.
The solution was to drag n drop the xcdatamodeld into the second project from the Finder directly and not from the first proj in xcode.
In this line:
NSString *localModelPath = [[NSBundle mainBundle] pathForResource:@"mupo-ios" ofType:@"momd"];
Change momd to mom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With