核心数据iOS10:viewContext没有使用NSFetchResultController从newBackgroundContext()接收更新 [英] Core Data iOS10: viewContext not receiving updates from newBackgroundContext() with NSFetchResultController

查看:209
本文介绍了核心数据iOS10:viewContext没有使用NSFetchResultController从newBackgroundContext()接收更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个 NSFetchResultController 来加载UITableView中的Core Data对象。
与此FRC关联的获取请求使用可用于 NSPersistentContainer (iOS10)的新 viewContext 属性。

In my application, I have a NSFetchResultController 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).

当我选择一个单元格时,我将Core Data对象传递给一个新的ViewController。这个新的VC仍然使用viewContext。
从这个ViewController,我可以从模态呈现的ViewControllers更新Core Data对象。为此,我使用 newBackgroundContext()作为模态ViewControllers。我可以毫无问题地保存更新Core Data对象。

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.

问题是FRC不会使用后台上下文中的更新Core Data对象自动更新。
就好像viewContext没有接收并处理Core Data对象更新。

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.

如果我设置 automaticMergesChangesFromParent 为true,FRC在保存背景上下文时获取更新的Core Data对象。根据我的理解,viewContext应该自动管理数据的合并。该文档描述了viewContext:此上下文配置为世代并自动使用来自其他上下文的保存通知。

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

你能来吗?阐明如何使用NSFetchResultController处理不同的上下文?

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

谢谢,
Axel

Thanks, Axel

推荐答案

你看到了正确的行为。如果您希望viewContext自动从其他上下文中获取更改,包括由 newBackgroundContext()创建的更改,则必须设置 automaticMergesChangesFromParent true

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

这篇关于核心数据iOS10:viewContext没有使用NSFetchResultController从newBackgroundContext()接收更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆