Even if you were to observe empirically that such a block were executed after the notification, to rely on that would be to assume undocumented implementation details. What is safe to rely on is that NSNotificationCenter sends its notifications synchronously (barring one specific case discussed below), so by adding another observer, and dispatching the block from there, you can be safer.
You might do this like (assuming ARC):
__block id token = [[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextObjectsDidChangeNotification object: moc1 queue: nil usingBlock:^(NSNotification *note) {
if (token)
{
id copyOfToken = token; // so it'll be const-copied into the next block, despite token being nilled out
dispatch_async(dispatch_get_main_queue(), ^{
[X iHopeYouGetThisAfterYouGotTheNotification];
[[NSNotificationCenter defaultCenter] removeObserver: copyOfToken];
});
token = nil;
}
}];
Since that notification will be delivered synchronously on the posting queue, you can be assured that -iHopeYouGetThisAfterYouGotTheNotification
will happen afterwards, regardless of the thread posting the notification.
Note that if X
were receiving the notification asynchronously (say, by subscribing to the notification using -addObserverForName:object:queue:usingBlock:
and having passed [NSOperationQueue mainQueue]
) then this would not be guaranteed to work, but that seems like an unlikely (and avoidable) arrangement.