gpt4 book ai didi

ios - 具有嵌套 PerformBlock 的核心数据

转载 作者:行者123 更新时间:2023-12-01 19:02:58 27 4
gpt4 key购买 nike

我读了一些文章,但仍然没有得出结论。假设我有一个嵌套的 NSManagedObjectContext

 BackgroundContext -> NSPrivateQueueConcurrencyType
|
child
v
MianContext -> NSMainQueueConcurrencyType
|
child
v
TemporaryContext -> NSPrivateQueueConcurrencyType

问题一:我还应该观察 NSManagedObjectContextObjectsDidChangeNotification并合并更改,例如:
- (void)managedObjectContextDidChanged:(NSNotification *)notification{
NSManagedObjectContext *currentContext = [notification object];
NSManagedObjectContext *mainConetext = [self mainThreadContext];
if (currentContext != mainConetext) {
[mainConetext performBlock:^{ //performBlock in notification
[mainConetext mergeChangesFromContextDidSaveNotification:notification];
}];
}
}

事实上,我已经尝试删除此代码,当我在 mainContext 上执行提取时,它表现良好。 ,但我感到困惑的是 mainContext如何合并子上下文变化 ,因为我觉得我在这里做错了什么。

问题二:我应该使用嵌套 performBlock合并更改??
[temporaryContext performBlock:^(
//do some thing
[mainContext performBlock:^(
// merge changes
)];
)];

最佳答案

保存子上下文时,这些更改会自动推送到父上下文。正如 docs 中所说,您可以将上下文堆栈视为在根部具有持久存储协调器。保存的更改会向上推一级:

When you save changes in a context, the changes are only committed “one store up.” If you save a child context, changes are pushed to its parent. Changes are not saved to the persistent store until the root context is saved. (A root managed object context is one whose parent context is nil.) In addition, a parent does not pull changes from children before it saves. You must save a child context if you want ultimately to commit the changes.



因此,如果您保存子上下文,这些更改将被推送到父上下文 - 但除非保 stub 上下文,否则不会将其写入磁盘,出于您的目的,这可以按不同的时间表进行。

在这种结构下,不需要合并更改和观察保存。这是它的众多优点之一。

关于ios - 具有嵌套 PerformBlock 的核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com