How to keep a child NSManagedObjectContext up to d

2019-05-14 17:17发布

I have a NSManagedObjectContext set to have a NSPrivateQueueConcurrencyType which I'm using most of the time across my app.

As well as this I created a child MOC with NSMainQueueConcurrencyType for use with cocoa bindings (I hear that bindings don't work with private queue MOCs). I have bound some ObjectControllers and an ArrayController to this child context. I very much want to keep the child on the main queue rather than swapping the MOC queue types.

When I make changes to the bound objects via UI, the changes don't propagate up to the parent context. And when I make changes to the parent context, they don't filter down to the Object/ArrayControllers.

How can I make this happen? Is there a setting that will tell the Object/ArrayControllers to refresh their context appropriately and save it when they make changes?

1条回答
来,给爷笑一个
2楼-- · 2019-05-14 17:37

To bring changes to the parent, you need to save the child. If you want to save the changes persistently, you also need to save the parent after that.

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

To bring changes from parent to the child, you need either merge changes using a notification method from parent's NSManagedObjectContextDidSaveNotification or re-fetch in child context. Merging is probably better in your case.

- (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];
    }];
}
查看更多
登录 后发表回答