Re: Notifications vs. messaging
Re: Notifications vs. messaging
- Subject: Re: Notifications vs. messaging
- From: Nat! <email@hidden>
- Date: Tue, 2 Oct 2001 00:41:47 +0200
Am Montag den, 1. Oktober 2001, um 13:01, schrieb Markus Hitter:
Am Montag, 1. Oktober 2001 um 00:41 schrieb Brian Hook:
Is there a simple guideline as to when to use a notification vs. when
to send a message directly?
sending a method directly:
- much less overhead
- only one object per call
- is executed immediately
- you can get a return value
sending a notification:
- some overhead for queueing
- can be received by several subscribers
- is queued and sent from within the run loop. This makes a difference,
for example if you want to open a sheet in -awakeFromNib .
An intermediate between both is to "inject" a method into the run loop:
[[NSRunLoop currentRunLoop]
performSelector:@selector(methodToPerform)
target:self argument:nil order:0
modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
or to use a NSTimer.
Well there is also the NSNotificationCenter, with which you can
broadcast notifications immediately. This is a synchronous process and
you can even make up a protocol, so that receivers supply the sender
with return values. So this is like a method call to many different
objects, you don't even know. The NSNotificationCenter is actually quite
speedy, the most time is probably wasted making up the various
notification objects.
As someone said this before:
message - you know (want to know) the receiver (usually coupling via
procotol or interface).
notification - you don't (want) to know the receiver(s) (loose coupling,
via string).
Nat!