[EDIT: simplified version of the question]
mainMOC
is the primary managed object contexteditorMOC
is a managed object context created ineditorViewController
with an undo manager so the user can edit a single managed objectafter
editorMOC
saves,mainMOC
refreshes the updated managed object in the notification handler forNSManagedObjectContextDidSaveNotification
In the save handler, if I use
[mainMOC refreshObject:obj mergeChanges:YES]
the updates to the object are not reflected inmainMOC
post-refresh. If I use[mainMOC refreshObject:obj mergeChanges:NO]
the object is invalidated and at the next fault the changes are reflected in the data loaded from the store.
QUESTION: Why would the object not reflect the update when mergeChanges:YES
is specified?
[ORIGINAL QUESTION]
I have a core data based app with multiple managed object contexts. The app is complicated and proprietary so I cannot simply share code directly from the app. I have created a simple test app in an attempt to reproduce my issue but the test app doesn't exhibit the problem. I have not been able to find a logical difference between the implementations. I apologize for not posting sample code, I believe I explained the implementation well below. If something is not clear, please ask in the comments and I will do my best to clarify.
Here's my situation. Everything described below is running on the main thread.
The app has a primary managed object context called
mainMOC
that is accessed on the main thread and used withNSFetchedResultsControllers
to display data in various table views.I have a view controller called
EditorViewController
that allows editing of an existing object of a particular entity. This view controller creates it's own managed object context callededitorMOC
using the same persistent store coordinator with an undo manager so changes can be rolled back or saved when dismissing the editor.EditorViewController
is observing theNSManagedObjectContextDidSaveNotification
. When this notification occurs, the notification handler calls[_mainMOC mergeChangesFromContextDidSaveNotification:notification]
to merge the changes fromeditorMOC
intomainMOC
.The table view controller that uses an
NSFetchedResultsController
is handling the controller delegate messages.
I have added NSLog output to look at look at what happens in all of the above steps and I have verified the following:
- I can see that the object is modified and saved in the editor.
- I can see that the
NSManagedObjectContextDidSaveNotification
is called and that the updated object is included. - I can see that the fetched results controller is receiving the
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
protocol message. - I can see that
mainMOC
is not reflecting the updates and that the updated object has not been invalidated bymergeChangesFromContextDidSaveNotification:
. - If I quit and relaunch the app, the updates were committed
For reference, both my main app and test app implement the above functionality but the test app shows the updates merged correctly and the main app does not.
I am looking for suggestions on what would cause mergeChangesFromContextDidSaveNotification:
not to successfully merge and/or invalidate the updated object.
Thanks!