Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning for iOS/iPhone users about duplicate NSNotification observations

Tags:

This isn't a question so much as a warning to others to save them some time.

NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior:

If you register yourself multiple times for the exact specific notification, NSNotificationCenter will NOT recognize the redundancy and instead will fire off as many notifications to you as you've registered an observation for.

This is almost never the behavior you want to see and is almost always accidental.

Example:

I want my view controller to receive notifications from a singleton network object when new data comes in:

- (void) viewDidLoad 
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:) 
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

but earlier I'd already put the same thing in viewWillAppear:

- (void) viewWillAppear
{
    [super viewWillAppear];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:)
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

Note that it's exactly the same notification, resolving to the same observer, sender and notification name.

In this case, if I don't remove one of those addObserver calls, I'll get duplicate notifications to my view controller.

In a multi-threaded environment, this is a world of hurt. Trust me.

Just putting this out there in case there are others who run into something like this.

like image 830
God of Biscuits Avatar asked Jun 18 '10 20:06

God of Biscuits


1 Answers

NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior:

If you register yourself multiple times for the exact specific notification, NSNotificationCenter will NOT recognize the redundancy and instead will fire off as many notifications to you as you've registered an observation for.

This is almost never the behavior you want to see and is almost always accidental.

Example:

I want my view controller to receive notifications from a singleton network object when new data comes in:

- (void) viewDidLoad 
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:) 
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

but earlier I'd already put the same thing in viewWillAppear:

- (void) viewWillAppear
{
    [super viewWillAppear];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:)
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

Note that it's exactly the same notification, resolving to the same observer, sender and notification name.

In this case, if I don't remove one of those addObserver calls, I'll get duplicate notifications to my view controller.

In a multi-threaded environment, this is a world of hurt. Trust me.

Just putting this out there in case there are others who run into something like this.

like image 173
God of Biscuits Avatar answered Sep 29 '22 18:09

God of Biscuits