13

I'm confused on why the observer is never removed in the following code. In my viewDidAppear I have the following:

-(void)viewDidAppear:(BOOL)animated{

id gpsObserver = [[NSNotificationCenter defaultCenter] 
                          addObserverForName:FI_NOTES[kNotificationsGPSUpdated] 
                          object:nil 
                          queue:[NSOperationQueue mainQueue] 
                          usingBlock:^(NSNotification *note){

                              NSLog(@"run once, and only once!");

                [[NSNotificationCenter defaultCenter] removeObserver:gpsObserver];

        }];

}

The observer never gets removed and the statement is output every time the notification is sent out. Can anyone provide any guidance?

warpedspeed
  • 1,098
  • 11
  • 28

2 Answers2

30

When the block is pushed onto the stack by addObserverForName: the method has not yet returned so gpsObserver is nil (under ARC) or garbage/undefined (not under ARC). Declare the variable using __block outside and this should work.

__block __weak id gpsObserver;

gpsObserver = [[NSNotificationCenter defaultCenter] 
                          addObserverForName:FI_NOTES[kNotificationsGPSUpdated] 
                          object:nil 
                          queue:[NSOperationQueue mainQueue] 
                          usingBlock:^(NSNotification *note){

                              NSLog(@"run once, and only once!");

                [[NSNotificationCenter defaultCenter] removeObserver:gpsObserver];

        }];

I've added an __weak to ensure there is no memory leak (as per Matt's answer). Code not tested.

Robotic Cat
  • 5,899
  • 4
  • 41
  • 58
11

I find that in fact there is a memory leak unless the observer is marked both __block and __weak. Use Instruments to make sure that self is not being overretained; I bet it is. This, however, works correctly (from my actual code):

__block __weak id observer = [[NSNotificationCenter defaultCenter] 
    addObserverForName:@"MyMandelbrotOperationFinished" 
    object:op queue:[NSOperationQueue mainQueue] 
    usingBlock:^(NSNotification *note) {
        // ... do stuff ...
        [[NSNotificationCenter defaultCenter] 
            removeObserver:observer 
            name:@"MyMandelbrotOperationFinished" 
            object:op];
}];
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I'd love it if they'd introduce the equivalent method that would actually not require you to persist the observers but rather use yourself as one, simply using blocks instead of selectors. – Eugene Mar 02 '14 at 18:02
  • 2
    You don't need to make `observer` `__weak` if you set it to `nil` after removing it from `NSNotificationCenter`. I've checked this with Instruments. – Ben Lings Mar 10 '14 at 15:03