Core Data iOS10: viewContext not receiving updates from newBackgroundContext() with NSFetchResultController

半城伤御伤魂 提交于 2019-12-02 16:16:23

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."

Automatically merge changes from parent needs to set on the viewContext like this:

persistentContainer.viewContext.automaticallyMergesChangesFromParent = true

I haven't run into this directly, but your issue strikes me as odd if the newBackgroundContext were in fact layered under the viewContext since any save from the new context would only update the viewContext, which would also have to do its own save to get the changes to the persistent store (which you say is occurring properly). Based on that suspicion, I looked at the developer docs where Apple says:

Invoking this method (newBackgroundContext() ) causes the persistent container to create and return a new NSManagedObjectContext with the concurrencyType set to privateQueueConcurrencyType. This new context will be associated with the NSPersistentStoreCoordinator directly and is set to consume NSManagedObjectContextDidSave broadcasts automatically.

Thus, it would not be in a parent - child relationship with the viewContext. Based on the guidance, it seems the new context would be notified of changes by the old, but not vice versa, so you'd have to do a refresh on the viewContext when the new context changes, which you could do programmatically if you can track that in your code or perhaps use one of the change notifications in NSManagedObjectsContext to trigger the action.

Not worked for me, So I changed save block from

self.persistentContainer.performBackgroundTask { (context) ... }

to

self.persistentContainer.newBackgroundContext().performAndWait { ... }

and of corse setting true automaticallyMergesChangesFromParent to make auto merge work.

lazy var viewContext: NSManagedObjectContext = {
    self.persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
    self.presistentContainer.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
    return self.persistentContainer.viewContext
}()

I don't know why the context from performBackgroundTask not merged to viewContext.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!