I need an advice for the following scenario. I have two managed object contexts in my app. Depending upon the thread they are accessed via same property i.e.
self.managedObjectContext
If any context changes or saves data i want to keep data sync on both contexts using
-refreshObject:mergeChanges:
and passing mergeChanges = YES, And the app is also listening for notification NSManagedObjectContextDidSaveNotification
. But i cannot know which context saved data. (or can i? if yes, Can anybody tell?)
My question is that on receiving this notification should i perform -refreshObject:mergeChanges:
on both contexts? e.g.
[bgContext refreshObject:self mergeChanges:YES];
[fgContext refreshObject:self mergeChanges:YES];
OR
[self.managedObjectContext refreshObject:self mergeChanges:YES];
or what approach should i follow?
In your call to NSNotificationCenter -addObserver:selector:name:object:
, provide the appropriate MOC as the :object
parameter. Now you'll only receive notifications for changes from that MOC. The object:
parameter works the same if you're using the blocks-based addObserver:...
call.
You could also use just the notfication, without registering diffferent selecotrs/blocks per context. When the notification arrives, you can check its object
property and use that to determine which context needs updating.