How to keep a child NSManagedObjectContext up to date when using bindings

北慕城南 提交于 2019-12-22 10:45:06

问题


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:


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];
    }];
}


来源:https://stackoverflow.com/questions/17540504/how-to-keep-a-child-nsmanagedobjectcontext-up-to-date-when-using-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!