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
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.
This crash appears whenever I want to access a property from CD object.
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).
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];
}];
});
}];
});
Update 1 Sometimes when doing the saveContext, I would get the following error:
"Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn't be completed. (Cocoa error 1550.)
Dangling reference to an invalid object.=null
NSLocalizedDescription=The operation couldn't be completed. (Cocoa error 1550.), NSValidationErrorValue=Relationship 'friends' on managed object (0x201cd340)
UID: <4C1B48C8-6309-4E8E-A590-DED497907A3A>. Asset ID: (null). with objects {(\n '(null)' UID: <(null)>.)}}"
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...