我创建了一个Core Data应用程序,然后从中获取了.xcdatamodel文件并将其添加到另一个iOS应用程序中。这个新应用程序是SingleView应用程序,我现在正在尝试连接到此.xcdatamodel文件以运行Core Data功能。我所做的是创建一个Singleton类,我通过它调用实例方法:
NSManagedObjectContext *context = [[SingletonDB sharedInstance] managedObjectContext];
managedObjectContext方法如下所示:
- (NSManagedObjectContext *) managedObjectContext{
static NSManagedObjectContext *context = nil;
if(context){
return context;
}
NSPersistentStoreCoordinator *coordinator = nil;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyTestDatabase" withExtension:@"momd"];
NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSLog(@"%@", [objectModel entities]);
...
}
在“managedObjectContext”方法中,由于某种原因,modelURL的值为nil,从而导致RuntimeException:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
我错过了导致此错误的步骤吗?当我选择MyTestDB.xcdatamodel文件时,我会看到我在之前的应用程序中创建的完整Core Data架构,现在由于某种原因我无法读取数据。我也尝试将扩展名从“momd”更改为“mom”,但这只会将RuntimeException更改为:
'NSInvalidArgumentException', reason: 'Cannot create an SQL store with a nil URL.'
任何人都可以看到问题所在吗?
感谢所有回复的人。
答案 0 :(得分:0)
试试这个,
+ (NSManagedObjectModel *)managedObjectModel
{
static NSManagedObjectModel *managedObjectModel;
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString * modelPath = [[NSBundle mainBundle] pathForResource:@"MyTestDatabase" ofType:@"momd"];
managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
return managedObjectModel;
}
+(NSPersistentStoreCoordinator *)persistentStoreCoordinator{
static NSPersistentStoreCoordinator * persistentStoreCoordinator;
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSError *error=nil;
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyTestDatabase.sqlite"];
NSURL *storeUrl =[NSURL fileURLWithPath:storePath];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
{
NSLog(@"store url %@",storeUrl);
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1);
}
return persistentStoreCoordinator;
}
+(NSManagedObjectContext *)managedObjectContext
{
static NSManagedObjectContext * managedObjectContext;
if(managedObjectContext!=nil){
return managedObjectContext;
}
@try {
NSPersistentStoreCoordinator * coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
}
@catch (NSException *exception) {
NSLog(@"Exception occur %@",exception);
}
return managedObjectContext;
}