Why doesn't Remove Observer from NSNotificatio

2020-01-31 02:18发布

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?

2条回答
做自己的国王
2楼-- · 2020-01-31 02:33

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];
}];
查看更多
女痞
3楼-- · 2020-01-31 02:56

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.

查看更多
登录 后发表回答