本文介绍了奇怪的父/子NSManagedObjectContext现象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了两个这样的上下文:

I have created two context like this:

// create writer MOC
_privateWriterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_privateWriterContext setPersistentStoreCoordinator:_persistentStoreCoordinator];

// create main thread MOC
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.parentContext = _privateWriterContext;

我有一个 NSFetchResultedController c $ c> _managedObjectContext 。

I have a NSFetchResultedController initiated with _managedObjectContext.

我知道这很奇怪,但我添加一个记录到父 _privateWriterContext ,我保存

I know it is strange, but I am adding a record to the parent to _privateWriterContext, I am saving it.

令人惊讶的是,因此 FRC 会收到有关此事件的通知。为什么?我没有重置的孩子,或任何其他。我认为他们是独立的实体,只要孩子的上下文不会得到保存。

What is surprising is that child context and so FRC gets notified about this event. Why? I have not reset-ed child, or anything else. I thought they are independent entities as long as child context will not get saved.

在@pteofil文章我发现这行:

In @pteofil article I found this line:

..它被推送到持久存储(通过持久存储协调器),并且对连接到存储的所有上下文都可见。

.. it is pushed to the persistent store (via the persistent store coordinator) and becomes visible to all contexts connected to the store.


推荐答案

我强烈建议避免父子环境设置。我们的书详细介绍了为什么它们常常导致奇怪的行为:

I strongly recommend avoiding parent-child context setups. Our book goes into detail why they often lead to strange behaviour: https://www.objc.io/books/core-data/

短篇小说:他们并不像你认为的那样独立。

Short story: They're not as independent as you might think.

如果您需要多个单一上下文,请使用共享一个持久性存储协调器的多个上下文。

Use multiple context that share a single persistent store coordinator if you need more than a single context.

这篇关于奇怪的父/子NSManagedObjectContext现象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 02:50