解决CoreData错误:NULL _cd_rawData但该对象不会变成故障 [英] Solving CoreData error: NULL _cd_rawData but the object is not being turned into a fault

查看:903
本文介绍了解决CoreData错误:NULL _cd_rawData但该对象不会变成故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当使用Core-Data对象时,应用程序崩溃时会出现错误:

Sometimes while using Core-Data object the app crashes with the error:


CoreData:error:NULL _cd_rawData但对象不是正在将
变成错误

CoreData: error: NULL _cd_rawData but the object is not being turned into a fault

从我可以从互联网上研究和阅读的是,对象上下文在两个线程之间传递,MOC不是线程安全的。

From what I could research and read from the internet, is that this happens when a managed object context is passed between two threads and MOCs are not thread safe.

每当我想从CD对象访问属性时,会出现此崩溃。

This crash appears whenever I want to access a property from CD object.

如果我有Person对象并且想要访问Perosn.name,应用程序可能会崩溃与这个错误(如前所述,它发生,只要我可以看到随机和I不能重现它,有时它会连续发生10次,然后不会发生一两天)。

If I have Person object and want to access Perosn.name the app could crash with this error (as said before, it happens as far as I can see randomly and I cannot reproduce it, sometimes it would happen 10 times in a row and then not happen for a day or two).

当看这个问题,似乎这发生当Person获取和更新Person的朋友关系(这是在后台线程上完成的,保存并合并到主线程MOC)。

When looking at this problem, it seems that this happens when Person I get and update Person's friends relationship (this is done on the background thread, saved and merged to the main thread MOC).

我想获得更多信息关于这里发生了什么,为什么这个错误发生,因为它看起来很随机,如果有任何方式来防止崩溃。

I would like to get more information about what is happening here, why does this error happen as it seems pretty randomly and if there is any way to prevent the crash.

下面是上下文的代码保存:

Below is the code where the context is saved:

__block MyAppDelegate *blockSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [blockSelf.managedObjectContext performBlock:^{
            [blockSelf.managedObjectContext save:nil];

            dispatch_async(blockSelf.core_data_queue, ^{
                [blockSelf.writerContext performBlock:^{
                    [blockSelf.writerContext save:nil];
                }];
            });
        }];
    });

更新1
有时在执行saveContext时,得到以下错误:

Update 1 Sometimes when doing the saveContext, I would get the following error:

错误域= NSCocoaErrorDomain代码= 1550
无法完成操作。 (可能错误1550)

"Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn't be completed. (Cocoa error 1550.)

无效对象的引用。= null

Dangling reference to an invalid object.=null

NSLocalizedDescription =不能完成。 (可能错误1550.),NSValidationErrorValue =受管对象(0x201cd340)上的关系朋友

NSLocalizedDescription=The operation couldn't be completed. (Cocoa error 1550.), NSValidationErrorValue=Relationship 'friends' on managed object (0x201cd340)

UID:< 4C1B48C8-6309-4E8E-A590-DED497907A3A>。资产ID:(null)。对象{(\\\
'(null)'UID:<(null)>。)}}

UID: <4C1B48C8-6309-4E8E-A590-DED497907A3A>. Asset ID: (null). with objects {(\n '(null)' UID: <(null)>.)}}"

我发现这个回答从另一个SO问题:
这是因为在不同的上下文中创建的对象被设置,注意不是在不同的线程上同一线程上的不同上下文。

I found this answer from another SO question: "It was due to the object being set created in a different context, note not on a different thread just a different context on the same thread."

这似乎是这种情况,如果它是如何找到对象是在不同的上下文中创建的,我试图保存到...

Does this seem to be the case, and if it is how could I find where the object is created in the different context that to what I am trying to save to...

推荐答案

嗯,在代码中我会改变一些东西。

Hmmm, some things I would change in that code.


  1. 通过一个 NSError 指针,检查从 - 返回的返回值,

  1. Use the error pointers, that is what they are for. You will probably get a solution just from that. Pass in an NSError pointer, check the return from the -save: call and spit out the error to console on a failure.

您的队列管理有点吓人,而不是执行 dispatch_async(),将其更改为 - [NSManagedObjectContext performBlock:] 这将确保你在正确的线程/您正在访问的上下文的队列。

Your queue management is a little scary. Instead of doing dispatch_async(), change that to -[NSManagedObjectContext performBlock:]. That will guarantee you are on the right thread/queue for the context you are accessing. With the way you have written it there is no guarantee and thus maintainability is low.

一旦你进行了这两个修改并且仍然失败,请使用 NSError 对象的输出更新您的问题,我们可以看到发生了什么。

Once you make those two changes and you are still failing, update your question with the output from the NSError objects and we can see what is going on.

即使保存中没有发生错误,仍然需要检查返回值和错误,因为它可以提供缺少的信息。

Even though the error is not happening on the saves, you still want to check that return value and the error as it can give us the missing information.

如果/当您重现崩溃,请在此回复。

Please reply here if/when you reproduce the crash.

好吧,这往往意味着你正在不同的MOCs创建对象,然后通过一个关系连接它们,因为你已经收集。你可以发布或描述如何以及何时创建对象?您使用什么MOC?

Ok, that tends to indicate that you are creating objects in different MOCs and then connecting them through a relationship as you gathered already. Can you post or describe how and when you are creating objects? What MOC you are using?

您还可以发布更新后的代码进行保存吗?

Can you also post your updated code for saving?

这篇关于解决CoreData错误:NULL _cd_rawData但该对象不会变成故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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