Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 814
XJones Avatar asked May 09 '11 19:05

XJones


2 Answers

The fetched results controller does not receive the controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: delegate message. Instead it sends it to the delegate in response to a change in the context. If that delegate message is being sent and does contain the proper object, then the main context is aware of the changes.

If so, then your error is most likely in the the code interfacing the fetched results controller and the tableview. Something is preventing the object from appearing correctly.

like image 178
TechZen Avatar answered Oct 21 '22 03:10

TechZen


The issue I had that caused mergeChangesFromContextDidSaveNotification not to work was I created a new NSPersistentStoreCoordinator for each new NSManagedObjectContext. When I shared NSPersistentStoreCoordinator between all MOCs, mergeChangesFromContextDidSaveNotification works perfectly. Also, make sure mergeChangesFromContextDidSaveNotification is called on the thread that owns the MOC.

From the research I did, it is safe to share NSPersistentStoreCoordinator between threads for use with NSManagedObjectContext. NSManagedObjectContext will lock the persistent store as necessary.

like image 34
Nicholas Palmer Avatar answered Oct 21 '22 04:10

Nicholas Palmer