Reputation: 4930
I got some troubles understanding how core data and references work together.
I would like to a subclass of a NSOperation to take as an argument a subclass of a NSManagedObject and use it during its execution.
The problem is this argument could have been updated, and even deleted, between the moment the operation is initialized and the moment the operation is executed.
How can I take those possible modifications in account during the execution of the operation ?
Thanks in advance
Upvotes: 0
Views: 47
Reputation: 28409
Unfortunately, there is no single answer, as it depends on how you anticipate using the data. There are lots of options, and I have learned that there is rarely a single universal truth.
However, there are some established principles.
Since you will be using an NSOperation, I suggest using a separate scratchpad MOC.
Now, you can query for changes. You can call existingObjectWithID:error:
which will usually let you know if the object has been deleted. You can also issue refreshObject:mergeChanges:
calls to bring in any updates. However, IMO, you are swimming against the tide.
In general, your application should monitor any MOC and handle the MOC notifications appropriately.
Marcus Zarra has a nice post on it here: http://www.cimgf.com/2014/02/25/deleting-objects-in-core-data/
Note that WWDC 2015 introduced a great new change that no longer throws when trying to access a deleted object, but it doesn't really help you deal with deletions per se... it just helps your app not crash if you don't handle them in any way.
Upvotes: 1