3

我有一套NSManagedObjectContextNSPrivateQueueConcurrencyType在我的应用程序中大部分时间都在使用的一套。

除此之外,我还创建了一个子 MOCNSMainQueueConcurrencyType用于可可绑定(我听说绑定不适用于私有队列 MOC)。我已经将一些ObjectControllers 和 an绑定ArrayController到这个子上下文。我非常希望将孩子保留在主队列中,而不是交换 MOC 队列类型。

当我通过 UI 对绑定对象进行更改时,这些更改不会传播到父上下文。当我对父上下文进行更改时,它们不会过滤到 Object/ArrayControllers。

我怎样才能做到这一点?是否有一个设置会告诉 Object/ArrayControllers 适当地刷新他们的上下文并在他们进行更改时保存它?

4

1 回答 1

6

要将更改带给父级,您需要保存子级。如果要持久保存更改,则还需要在此之后保存父级。

[child save:&error];
[parent performBlock:^{
    [parent save:&parentError];
}];

要将更改从父级带到子级,您需要使用父级的通知方法合并更改NSManagedObjectContextDidSaveNotification或在子上下文中重新获取。在您的情况下,合并可能更好。

- (void)managedObjectContextDidSave:(NSNotification *)notification {
    // Here we assume that this is a did-save notification from the parent.
    // Because parent is of private queue concurrency type, we are
    // on a background thread and can't use child (which is of main queue
    // concurrency type) directly.
    [child performBlock:^{
        [child mergeChangesFromContextDidSaveNotification:notification];
    }];
}
于 2013-07-09T09:15:47.973 回答