Friday 27 September 2013

Receiving NSNotification on different queue from where was sent, and serial message receipt

Receiving NSNotification on different queue from where was sent, and
serial message receipt

How do you guarantee that a message is sent to an object after that object
has received a notification on a different thread?
Background:
Consider two objects, X and Y. X posts an NSNotification through
NSNotificationCenter on thread Q. Y is registered as an observer of that
notification, but on thread P. Like this:
Y.m
dispatch_async(P, ^{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handler:) name:MyNotification object:X];
};
- (void)handler:notif { ... }
- (void)iHopeYouGetThisMessageAfterYouGotMyNotification:sender
didYou:(BOOL *)b { ... }
X.m
dispatch_async(Q, ^{
[[NSNotificationCenter defaultCenter]
postNotificationName:MyNotification object:self]
[Y iHopeYouGetThisMessageAfterYouGotMyNotification:self didYou:NULL];
};
The question is: if X sends a message to Y after posting the notification,
how do you ensure Y gets the message after observing the notification, not
before?

No comments:

Post a Comment