Search code examples
iphoneioscore-datansmanagedobjectcontext

What could cause mergeChangesFromContextDidSaveNotification not to merge/invalidate objects that have been updated?


[EDIT: simplified version of the question]

  1. mainMOC is the primary managed object context

  2. editorMOC is a managed object context created in editorViewController with an undo manager so the user can edit a single managed object

  3. after editorMOC saves, mainMOC refreshes the updated managed object in the notification handler for NSManagedObjectContextDidSaveNotification

  4. In the save handler, if I use [mainMOC refreshObject:obj mergeChanges:YES] the updates to the object are not reflected in mainMOC 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.

  1. The app has a primary managed object context called mainMOC that is accessed on the main thread and used with NSFetchedResultsControllers to display data in various table views.

  2. 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 called editorMOC using the same persistent store coordinator with an undo manager so changes can be rolled back or saved when dismissing the editor.

  3. EditorViewController is observing the NSManagedObjectContextDidSaveNotification. When this notification occurs, the notification handler calls [_mainMOC mergeChangesFromContextDidSaveNotification:notification] to merge the changes from editorMOC into mainMOC.

  4. 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 by mergeChangesFromContextDidSaveNotification:.
  • 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!


Solution

  • In an effort to accept something even though I don't have a definitive solution, here's what I've done to address this and the problem seems to be resolved.

    I believe this was a cache issue with NSFetchedResultsController. I've since simplified this code and made sure that each NSFetchedResultsController uses it's own cache. I've been able to remove the call to 'refreshObject' and things seem to be working correctly.