无法创build具有nil模型错误的NSPersistentStoreCoordinator

我有一个完美运行的coreData datamodel文件。 由于一些特殊的要求,我删除了旧的datamodel文件,并创build了完全相同的实体的另一个datamodel文件。 以前的dataModel 实体没有变化 。 我已经把它作为一个不同的包的一部分,并从该包中引用它。

创buildmanagedObjectModel的代码

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)` 

创build持久性商店协调员的代码

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

这个错误发生在一个随机点,但是每次运行应用程序时都非常一致。 我完全困惑,并在这一点上…在论坛上我看到了同样的问题,但我想我的是一个特殊的场景。 我很确定,上面的代码片段中提到的modelPathvariables每次打印时都会很好。

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

首先validationmanagedObjectModel_是否有效,您可以使用debugging器或调用它的方法

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

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

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

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

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

并删除.sqlite文件,这样核心数据将在下次生成时新生成。

实际的问题原来是一个内存问题。 我没有使用来自AppDelegate的核心数据对象,我在其他一些类中创build它们。 但是,我还没有发布这些对象,因为有一个巨大的泄漏,应用程序崩溃。 我发布了核心数据对象,现在我的应用程序工作得很好…

如果您的datamodel名称是Model.xcdatamodeid。 只需在AppDelegate.h文件中将URLForResource参数replace为您的数据模型名称即可。

NSURL * modelURL = [[NSBundle mainBundle] URLForResource:@“Model”withExtension:@“momd”];

下面的代码供参考。

  • (NSManagedObjectModel *)managedObjectModel {

    //应用程序的托pipe对象模型。 这是一个致命的错误

应用程序无法find并加载其模型。

 if (_managedObjectModel != nil) { return _managedObjectModel; } 

NSURL * modelURL = [[NSBundle mainBundle] URLForResource:@“Model”withExtension:@“momd”];

 _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return _managedObjectModel; 

}