Search code examples
iphoneobjective-ciosxcodensnotifications

Notification is received more often than it's sent


I'm having a strange problem.

I have a method that sends off notifications by calling into the notification center like this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"NIDNewDataSetNotification" object:self];

If I NSLog the time right before sending the notification, I see that it sends every second, as it should.

I have a method in another object that adds itself as an observer like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDataNotification:) name:@"NIDNewDataSetNotification" object:nil];

However, by NSLogging the time, I see that it receives the notification four times every second (all four within like .001 seconds).

There are not other notifications being sent. If I comment out the line that sends the notification, my method that catches the notification does not get called.

Any ideas?


Solution

  • I can think of two cases where this might happen:

    1) Are you somehow registering for the notification multiple times? Maybe throw an NSLog(@"registering...") statement right before you register to make sure you only see it once.

    2) Are you unregistering for the event? Could that be broken or not called? Maybe put an NSLog statement there too to make sure it's getting called, and make sure you're removing the observer for the correct delegate & notification.