无法创建具有nil模型错误的NSPersistentStoreCoordinator [英] Cannot create an NSPersistentStoreCoordinator with a nil model error

查看:144
本文介绍了无法创建具有nil模型错误的NSPersistentStoreCoordinator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个coreData数据模型文件,运行完美。由于某些特殊要求,我删除了旧的数据模型文件,并使用完全相同的实体创建了另一个数据模型文件。来自先前的dataModel的实体没有变化。我已将其作为不同的软件包的一部分,并将其从该软件包中引用。

I have a coreData datamodel file which was running perfectly. Due to some special requirements i deleted the old datamodel file and created another datamodel file with exactly same entities. There is no change in entities from the previous dataModel. I have made this a part of a different bundle and referring it from that 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 变量在每次打印时都很好。

This error occurs at a random point but is very much consistent every time i run the app. I am totally confused and struck at this point... I have seen the same problem in the forum but i guess mine is a special scenario. I am pretty sure that the modelPath variable mentioned in the above code snippet is coming fine every time i print it.

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

推荐答案

出来是一个内存问题。我没有使用来自AppDelegate的核心数据对象,我在一些其他类中创建它们。但我没有发布这些对象,由于有一个巨大的泄漏,应用程序崩溃。我发布了核心数据对象,现在我的应用程序工作完美... ...

The actual problem turned out to be a memory issue. I was not using core data objects from AppDelegate and i am creating them in some other classes. But i have not released these objects due to which there was a huge leak and the app got crashed. I released the core data objects and now my app is working perfectly fine...

这篇关于无法创建具有nil模型错误的NSPersistentStoreCoordinator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆