问题
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 ObjectController
s 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