I have a main managed object context (MOC) in the UI thread which is usually updated by worker threads. Works fine!
However, I have one view where I create another MOC (called editingMOC) in the UI thread. I read myObject via objectID in editingMOC. Afterwards I do some modifications on the object myObject in the editingMOC. If the user hits cancel, the following happens:
[self.editingMOC reset];
myObjectEdit = [self.editingMOC existingObjectWithID:myObject.objectID error:NULL];
Shouldn’t that restore myObjectEdit to it’s state as it was before? I did not call save. Nevertheless myObjectEdit has still my changes. Any idea what might be wrong?
Thank you!
Update:
Apparently the refresh of myObjectEdit is correct (Thanks Mark Adams). I tracked it down to this weird behaivor:
// RESET VALUE - OK
NSLog(@"%@", myEditObject);
self.tableView.userInteractionEnabled = NO;
// OLD VALUE AGAIN - WRONG
NSLog(@"%@", myEditObject);
I haven’t changed anything regarding to userInteractionEnabled. Does this method have any side effects not stated in the docs?
You need to send
-refreshObject:mergeChanges:to yourNSManagedObjectContext. Pass in yourNSManagedObjectsubclass and passNOtomergeChanges:. This turns the object back into a fault and discards any changes since it was last fetched from a persistent store. In this case, the object has never been in a store, so it is completely discarded.NSManagedObjectContext Class Reference –
refreshObject:mergeChanges:Edit: It seems from your question that you may be passing objects around between threads. In that case, be sure to create the object by objectID instead of referencing the object across threads. I’m assuming you already know this though…