managedObjectModel为nil(无法使用nil模型创建NSPersistentStoreCoordinator) [英] managedObjectModel is nil (Cannot create an NSPersistentStoreCoordinator with a nil model)

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

问题描述

我有这种特殊情况,其中我的应用程序在一段时间内运行良好,一段时间后崩溃并不一致.我在崩溃期间遇到的错误是无法使用零模型创建NSPersistentStoreCoordinator"..

I have this special case scenario, where in my app works perfectly fine for some time and crashes inconsistently after some time. The error that i get during the crash is "Cannot create an NSPersistentStoreCoordinator with a nil model".

我尝试调试我的应用程序,发现managedObjectModel有时返回NULL.为了给火上加油,这种情况根本不一致.在一段时间内managedObjectModel没问题.但是,突然返回NULL ...

I tried debugging my app and found that the managedObjectModel is returning NULL sometimes. To add fuel to the fire, this scenario is not at all consistent. For some time the managedObjectModel is fine. But, suddenly it returns NULL...

这是我用来创建托管对象模型的代码.

Here is the code that I am using to create a managed object model.

- (NSManagedObjectModel *)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];

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

    return managedObjectModel_;
}

如上所示,由于某些特殊要求,我将. xcdataModeld 文件放置在单独的包中,并从那里进行引用.我很震惊,需要一些帮助....谢谢

As you can see above, due to some special requirements, I have placed my .xcdataModeld file in a separate bundle and referencing it from there. I got struck and need some help.... Thanks

推荐答案

首先,请确保不要将managedObjectModel_重置为nil.只需在源代码中搜索"managedObjectModel_ =",唯一的结果应该是您发布的managedObjectModel代码.

First of all, make sure you never reset managedObjectModel_ to nil. Just search your source code for "managedObjectModel_ =", the only result should be in the managedObjectModel code you posted.

其次,请确保从外部无法访问managedObjectModel_或(如果将managedObjectModel作为属性公开)是只读的.

Secondly, make sure that managedObjectModel_ is either inaccessible from outside or (if you expose managedObjectModel as a property) readonly.

第三,确保管理核心数据堆栈的类只有一个且只有一个实例.如果它是在主窗口笔尖中初始化的UIApplication委托,则不应以编程方式创建它.如果是单例,请检查是否确实有一个实例.

Thirdly, make sure there's one and only one instance of the class managing the Core Data stack. If it's the UIApplication delegate initialized in the main window nib, you shouldn't create it programmatically. If it's a singleton, check if there's really a single instance of it.

当您完全确定一切正确时,就该进行更深入的研究了.您可以尝试将GDB中的监视点设置为managedObjectModel_.

When you are absolutely sure everything is right, it's time to go digging deeper. You can try setting a watchpoint in GDB to managedObjectModel_.

代码中可能发生的最糟糕的事情是一种memset/memmove操作,这些操作恰好会覆盖您的Core Data Stack Manager占用的内存.但是这种错误太随机了,无法总是命中给定的内存地址,因此我不会指望它.

The worst thing possibly going on in your code is a kind of memset/memmove operations which happen to overwrite memory occupied by your Core Data stack manager. But this sort of error is too random to always hit a given memory address, so I wouldn't count on it.

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

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