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

问题描述

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

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

当我选择一个单元格时,会将Core Data对象传递给新的ViewController。此新的VC仍使用viewContext。
从此ViewController中,我可以从以模态显示的ViewControllers中更新Core Data对象。为此,我对模式ViewController使用 newBackgroundContext()。我可以毫无问题地保存更新核心数据对象。

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不会自动从后台上下文中用更新核心数据对象进行更新。
好​​像viewContext没有接收并处理核心数据对象更新。

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.

如果我设置 automaticallyMergesChangesFromParent 对于viewContext(应用程序范围)为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."

阐明如何使用NSFetchedResultsController处理不同的上下文?

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

推荐答案

您看到的是正确的行为。如果您希望viewContext自动从其他上下文中获取更改,包括由 newBackgroundContext()创建的更改,则必须设置 automaticallyMergesChangesFromParent 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."

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

08-14 12:47