尝试在 iOS 中连接 ManagedObjectContext 时出现运行时异常
Posted
技术标签:
【中文标题】尝试在 iOS 中连接 ManagedObjectContext 时出现运行时异常【英文标题】:Getting runtime exception when trying to connect with ManagedObjectContext in iOS 【发布时间】:2013-09-26 03:49:19 【问题描述】:我创建了一个 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 文件时,我看到了我在之前的应用程序中创建的完整核心数据模式,但现在由于某种原因我无法读取数据。我还尝试将扩展名从“momd”更改为“mom”,但这只会将 RuntimeException 更改为:
'NSInvalidArgumentException', reason: 'Cannot create an SQL store with a nil URL.'
可能是什么问题?
【问题讨论】:
您是否验证了MyTestDatabase.mom
确实存在于应用程序包中?
【参考方案1】:
试试这个,
+ (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;
【讨论】:
以上是关于尝试在 iOS 中连接 ManagedObjectContext 时出现运行时异常的主要内容,如果未能解决你的问题,请参考以下文章
将 managedobject 从 uitableview 传递到文本字段以在核心数据模型中进行编辑