43

I occasionally get this error when saving to core data. I cannot manage to recreate it.

Does anyone have any experience with this error;

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

?

johan
  • 6,578
  • 6
  • 46
  • 68

3 Answers3

57

Reason:

This happens when the object's context does not belong to the current thread. One of many ways that mistake can manifest itself.

Debug:

When the crash occurs do the following:

  1. Check the thread in which the crash occured (Xcode > Debug Navigator)
  2. On the console print, the concurrency type of the NSManagedObjectContext
  3. If the concurrency type is mainQueueConcurrencyType then it needs to be on the main thread otherwise on the background threads.

Console command to check concurrency type:

p context.concurrencyType

In the sample command above the NSManagedObjectContext was stored in the variable context

Console Output:

(NSManagedObjectContextConcurrencyType) $R4 = mainQueueConcurrencyType
Community
  • 1
  • 1
Neal Ehardt
  • 10,334
  • 9
  • 41
  • 51
  • 2
    Yes this was exactly my case. This error is basically because of accessing the main MOC from the background thread. – Roohul Dec 26 '16 at 07:48
  • 1
    Is there any solution for this? – Kirti Nikam Jul 12 '17 at 11:10
  • 3
    If you need to access the database from a background thread, use that thread to create a new MOC, then use it to create, query, mutate, save, etc. [MagicalRecord](https://github.com/magicalpanda/MagicalRecord) was very helpful for my app. – Neal Ehardt Jul 14 '17 at 22:46
  • 1
    In my case, It was already on the background thread but the frequent switching from the backgrounf thread to main thread causes this error and a crash – Vin Feb 28 '19 at 07:44
14

After going to several post, this had a better answer https://web.archive.org/web/20150215081345/http://www.cocoabuilder.com:80/archive/cocoa/311615-weird-core-data-crash.html

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
iamsult
  • 1,581
  • 1
  • 15
  • 21
1

I found the same problem when I tried to read (access) data from a Coredata Store in other thread (not main).

You can solve this problem by following this suggestion from this link:
Core Data and threads / Grand Central Dispatch

lewis
  • 2,936
  • 2
  • 37
  • 72
Ohmy
  • 2,201
  • 21
  • 24