[EDIT: simplified version of the question]
mainMOC is the main context of the managed entity.
editorMOC is a managed object context created in editorViewController with an undo manager, so the user can edit one managed object
after editorMOC saves mainMOC updates the updated managed entity in the notification handler for NSManagedObjectContextDidSaveNotification
In the save handler, if I use [mainMOC refreshObject:obj mergeChanges:YES] , updates for the object are not reflected in mainMOC after the update. If I use [mainMOC refreshObject:obj mergeChanges:NO] , the object is invalid and the next time it fails, the changes are reflected in the data downloaded from the repository.
QUESTION: Why does the object not reflect the update when specifying mergeChanges:YES ?
[ORIGINAL QUESTION]
I have a basic data-based application with several contexts of managed objects. The application is complex and proprietary, so I can’t just pass the code directly from the application. I created a simple test application trying to reproduce my problem, but the test application does not detect the problem. I could not find the logical difference between the implementations. I apologize for not sending the sample code, I suppose I explained its implementation below. If something is unclear, ask in the comments and I will do my best to clarify.
Here is my situation. Everything described below is done in the main thread.
The application has a primary managed object context called mainMOC , accessed by the main thread and used with NSFetchedResultsControllers to display data in various table views.
I have a controller view editorViewController that allows me to edit an existing object of a specific object. This view manager creates its own managed object context called editorMOC using the same persistent repository coordinator with the undo manager, so changes can be discarded or saved when the editor is canceled.
editorViewController oversees NSManagedObjectContextDidSaveNotification . When this notification occurs, the notification handler calls [_mainMOC mergeChangesFromContextDidSaveNotification:notification] to merge the changes with editorMOC into mainMOC .
A table view controller that uses the NSFetchedResultsController handles controller delegate messages.
I added the NSLog output to see what happens in all of the above steps, and I checked the following:
- I see that the object is modified and saved in the editor.
- I see that
NSManagedObjectContextDidSaveNotification is called and the updated object is enabled. - I see that the resulting result controller is receiving the
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: protocol message. - I see that
mainMOC not reflective of updates, and that the updated object was not invalidated by mergeChangesFromContextDidSaveNotification: - If I leave and restart the application, updates have been fixed
For reference, both my main application and the test application implement the above functions, but the test application shows that the updates are merged correctly, and the main application does not work.
I am looking for suggestions about what could lead to the fact that mergeChangesFromContextDidSaveNotification: will not be able to successfully merge and / or invalidate the updated object.
Thanks!
Xjones
source share