Question

Ok, feeling pretty stupid here - but for some reason I just can't get my object to stop receiving notifications!

I've got a very basic setup in my init: method that's supposed to listen for the first notification and stop listening when received the first time. The init: methods is definitely called just once.

The issue is that it keeps receiving notifications after the first one:

   [[NSNotificationCenter defaultCenter] addObserverForName:kMyNotification
                                                     object:nil
                                                      queue:nil
                                                 usingBlock:^(NSNotification *note)
                                                           {

                                                              [NSLog(@"Got here"); 
                                                              [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                                                              name:kMyNotification
                                                                                                            object:nil];
                                                           }];

Any ideas why the removeObserver:name:object: call doesn't seem to have any effect?

Was it helpful?

Solution

Try this:

__block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kMyNotification
                                                 object:nil
                                                  queue:nil
                                             usingBlock:^(NSNotification *note)
                                                       {

                                                          [NSLog(@"Got here"); 
                                                          [[NSNotificationCenter defaultCenter] removeObserver:observer
                                                                                                          name:kMyNotification
                                                                                                        object:nil];
                                                       }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top