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

后端 未结 3 1686
粉色の甜心
粉色の甜心 2021-02-07 23:12

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

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

相关标签:
3条回答
  • 2021-02-07 23:33

    Hmmm, some things I would change in that code.

    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.

    2. 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.

    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.

    Update 1

    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.

    Update 2

    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?

    0 讨论(0)
  • 2021-02-07 23:33

    Facing same issue but solved by setting this in Fetchrequest

    [fetchrequest setReturnsObjectsAsFaults:NO];
    
    0 讨论(0)
  • 2021-02-07 23:42

    I have encountered this problem in my code as well. Later I noticed that I used a managed object created by main thread NSManagedObjectContext in background thread. Since managed object is notoriously thread unsafe so I changed my code and then this bug never appeared again and code works very well. So I guess this is IOS's way of complaining using managed object in threads other than the thread it has been created or fetched.

    Hope this help.

    0 讨论(0)
提交回复
热议问题