【发布时间】:2012-02-06 22:32:47
【问题描述】:
我有一个运行良好的 coreData 数据模型文件。由于一些特殊要求,我删除了旧的数据模型文件并创建了另一个具有完全相同的实体的数据模型文件。与之前的数据模型相比,实体没有变化。我已将此作为不同捆绑包的一部分,并从该捆绑包中引用它。
创建 managedObjectModel 的代码
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSBundle *newBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"dataBundle" withExtension:@"bundle"]];
NSString *modelPath = [newBundle pathForResource:@"DataHouse" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
该应用程序一直运行良好,但突然(随机)我收到一条错误消息
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
*** First throw call stack:`(0x62e052 0x26a9d0a 0xf6e86d 0x64fd 0x624e 0x381b 0x79c9b 0x65f2d 0x1881e0f 0x1882589 0x186ddfd 0x187c851 0x1827322 0x62fe72 0x160892d 0x1612827 0x1598fa7 0x159aea6 0x163437a 0x16341af 0x602966 0x602407 0x5657c0 0x564db4 0x564ccb 0x2791879 0x279193e 0x17e8a9b 0x28a2 0x2815)`
创建持久存储协调器的代码
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DataHouse.sqlite"]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
此错误发生在随机点,但每次运行应用程序时都非常一致。 在这一点上,我完全感到困惑和震惊……我在论坛上看到了同样的问题,但我想我的情况是特殊情况。我很确定上述代码 sn-p 中提到的 modelPath 变量在我每次打印时都会正常运行。
注意:上面的代码 sn-ps 不是 AppDelegate 类的一部分。它们是包含所有 Coredata 方法的特殊类的一部分
【问题讨论】:
-
请包含您创建持久存储协调器的代码
-
jrturton:请用代码检查我更新的问题
-
您需要使用调试器逐步完成它 - 在某个地方,您最终会发现模型没有被返回,但是没有任何东西跳出来,对不起!
-
感谢兄弟的尝试..
标签: iphone ios core-data xcode4.2 core-data-migration