Debugging NSObjectInaccessibleException – The NSManagedObject with ID:0x123456789 has been invalidated
I have a bug I’m struggling to track down. I believe what’s happening is that I’m deleting an object from the underlying database whilst another managed object context (in another thread) has a fault on it and gets the ‘NSObjectInaccessibleException’ when it tries to fulfil the fault.
The scenario is that I have a view accessing the data through one context meanwhile in the background, another threat is purging out of date records from the store. The background thread should only be purging objects which are not required by the view – this obviously isn’t the case but I’m having trouble tracking down exactly what happens. By the time I see the defect, it’s too late and it is a relatively rare defect that mainly only happens in the field.
Hence my question: Are there any tricks I’m missing when debugging CoreData – can I track lifetimes of objects from one context in another? I.e. when I delete my object is there an easy way to see if any other contexts have a reference to that same object? Using that, I could build some test code to check my logic and find the error.