Ios – Core Data viewContext not receiving updates from newBackgroundContext() with NSFetchedResultsController

core-dataiosios10nsfetchedresultscontrollernsmanagedobjectcontext

In my application, I have a NSFetchedResultsController to load Core Data objects in a UITableView.
The fetch request associated with this FRC uses the new viewContext property available for the NSPersistentContainer (iOS10).

When I select a cell, I pass the Core Data object to a new ViewController. This new VC still uses the viewContext.
From this ViewController, I can update the Core Data object from ViewControllers presented modally. To do so, I use newBackgroundContext() for the modal ViewControllers. I can save the update Core Data object without any issue.

The problem is that the FRC is not automatically updated with the update Core Data object from the background context.
It is as if the viewContext was not receivng and handling the Core Data object updates.

If I set automaticallyMergesChangesFromParent to true for the viewContext (app wide), the FRC gets the updated Core Data object when I save the background context. From my understanding, the viewContext should manage the merge of data automatically. The documentation describes the viewContext with: "This context is configured to be generational and to automatically consume save notifications from other contexts."

Can you clarify how to handle the different contexts with a NSFetchedResultsController?

Best Answer

You're seeing the correct behavior. If you want your viewContext to automatically pick up changes from other contexts, including ones created by newBackgroundContext(), you have to set automaticallyMergesChangesFromParent to true.

I agree that the docs are confusing on that point, "...and to automatically consume save notifications from other contexts."