我的应用程序使用CoreData
名为 : 的模型mupo-ios.xcdatamodel
。这个模型是原始模型的第二个版本:Model.xcdatamodeld。我正在使用managedObjectContext
通过文档:
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;
}
后来,我访问managedObjectContext
:
self.managedObjectContext = self.document.managedObjectContext;
现在,我想直接访问主上下文,代码如下:
- (NSManagedObjectContext *)mainContext {
if (!_mainContext) {
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_mainContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
}
return _mainContext;
}
问题是当我尝试指定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;
}
找不到 momd 的文件,因此managedObjectModel
无法初始化。有人可以帮助我吗?