iphone - 无法创建模型错误为 nil 的 NSPersistentStoreCoordinator

标签 iphone ios core-data xcode4.2 core-data-migration

我有一个运行完美的 coreData 数据模型文件。由于一些特殊要求,我删除旧的数据模型文件并创建了另一个具有完全相同实体的数据模型文件。与之前的数据模型相比,实体没有变化。我已将其作为不同 bundle 的一部分,并从该 bundle 中引用它。

创建 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_;

此错误随机发生,但每次运行该应用程序时都非常一致。 在这一点上,我完全感到困惑和震惊......我在论坛上看到了同样的问题,但我想我的情况比较特殊。我很确定上面代码片段中提到的 modelPath 变量在我每次打印时都很好。

注意:以上代码片段不是 AppDelegate 类的一部分。它们是包含所有 Coredata 方法的特殊类的一部分

最佳答案

首先验证 managedObjectModel_ 是否有效,您可以使用调试器或在其上调用方法

NSLog(@"%@", [managedObjectModel_ entities]);

验证您的数据模型是否正常。该调用应显示模型中所有实体的数组。接下来检查您的持久存储路径是否指向正确的位置。试试这个:

NSLog(@"%@", [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DataHouse.sqlite"]);

最后(我认为这是需要做的,因为它发生在我身上)转到磁盘上存储 .sqlite 文件的位置:

~/Library/Application Support/iPhone Simulator/5.0/Applications/{your app id}/Documents

并删除 .sqlite 文件,这样 Core Data 会在您下次构建时重新生成它。

关于iphone - 无法创建模型错误为 nil 的 NSPersistentStoreCoordinator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8804541/

相关文章:

iphone - 使用 UIViewContentModeScaleAspectFit 填充 View ?

javascript - 使用 UIWebview 检索 HTML 标签

objective-c - NSSortDescriptor 问题

iphone - 如何在iPhone图库中存储高质量的图像?

iphone - 使用iPhone的ImageMagick读取Dicom图像

iphone - 将登录屏幕显示为模态视图

ios - 如何在没有开发人员证书的情况下在 Xcode 中存档我的项目?

ios - 连续旋转不居中

ios - CoreData 失去关系 - 仅在重新加载后

ios - iCloud 中的 CoreData 和照片问题