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?
When removing an observer, remove it with the most specific detail possible. For example, if you used a name and object to register the observer, use removeObserver:name:object: with the name and object.
Why we should remove observer, Yes, we don't have to remove observers from From OS X 10.11 and iOS 9.0. This is not applicable for block based approach. Even when you use [weak self] remove the observer (or) make sure you never use shared instances within this closure.
specifies. If your app targets iOS 9.0 and later or macOS 10.11 and later, and you used addObserver(_:selector:name:object:) , you do not need to unregister the observer. If you forget or are unable to remove the observer, the system cleans up the next time it would have posted to it.
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.
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];
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With