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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to