Re: NSNotificationCenter : multiple messages sent to the same observer?
Re: NSNotificationCenter : multiple messages sent to the same observer?
- Subject: Re: NSNotificationCenter : multiple messages sent to the same observer?
- From: Bill Bumgarner <email@hidden>
- Date: Sun, 22 Feb 2009 10:48:26 -0800
On Feb 22, 2009, at 10:23 AM, Iceberg-Dev wrote:
If the notification center coalesced observers on registration, the
above would mean that the notification would no longer be sent.
If anObject is the same object in the 3 calls, this is probably
already the case if I am to believe the documentation. Which is the
behavior I would expect/wish for.
And you'd be correct. I tested it (see below).
[Session started at 2009-02-22 10:43:24 -0800.]
2009-02-22 10:43:24.137 foobar[11347:10b] Notif: {
times = 2;
}
2009-02-22 10:43:24.142 foobar[11347:10b] Notif: {
times = 2;
}
The Debugger has exited with status 0.
So... it'd appear that NSNotificationCenter is implemented entirely
around being as small and fast as possible. "Remove" removes
everything matching and "add" adds without consideration. No stack
or reference counting involved.
And you are correct that the documentation says that: "Removes
matching entries from the receiver’s dispatch table." Which, given
the minimal and similar description on -addObserver: would imply that
it adds the same without consideration for what is already in the
center.
b.bum
(The code below assumes GC is on, obviously. And it is only written
to test the question at hand -- not to even remotely be a correct
pattern of implementation).
#import <Foundation/Foundation.h>
#define BobsYourUncle @"BobsYourUncle"
@interface Foo: NSObject
@end
@implementation Foo
- (void) bob: (NSNotification *) aNotif
{
NSLog(@"Notif: %@", [aNotif userInfo]);
}
- init
{
NSNotificationCenter *dc = [NSNotificationCenter defaultCenter];
NSNotification *n;
[super init];
[dc addObserver:self selector:@selector(bob:) name:BobsYourUncle
object:self];
[dc addObserver:self selector:@selector(bob:) name:BobsYourUncle
object:self];
n = [NSNotification notificationWithName:BobsYourUncle
object:self userInfo: [NSDictionary dictionaryWithObject:@"2"
forKey:@"times"]];
[dc postNotification: n];
[dc removeObserver:self name:BobsYourUncle object:self];
n = [NSNotification notificationWithName:BobsYourUncle
object:self userInfo: [NSDictionary dictionaryWithObject:@"1"
forKey:@"times"]];
[dc postNotification: n];
return self;
}
@end
int main (int argc, const char * argv[]) {
objc_startCollectorThread();
[Foo new];
return 0;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden