Adding and removing observers to NSNotificationCenter in a UIViewController
Asked Answered
L

5

19

Looking at various Apple examples (for example Add Music) in which I see they add observers to the default NSNotificationCenter in viewDidLoad, then remove them in dealloc. This seems dangerous as viewDidLoad can be called multiple times without dealloc being called. This would then add the same observer multiple times, causing the handler to be called multiple times.

A solution to this would be to also remove observers in viewDidUnload, but this would then mean the same observer could be removed for a second time in dealloc which seems like a potential problem.

What am I missing?

Leilanileininger answered 26/4, 2012 at 12:13 Comment(3)
I don't think removing an observer which doesn't exist has any adverse effect.Wing
@Wing Thanks. I'm definitely thinking removing observers in both dealloc and viewDidUnload is the only way to go.Leilanileininger
https://mcmap.net/q/435820/-ios-remove-observer-from-notification-can-i-call-this-once-for-all-observers-and-even-if-there-are-none Updated answer.Monomorphic
P
25

There are a lot of discussions about removing notifications in the right way. For example:

I suggest you to remove observers in viewWillDisappear (or viewDidDisappear) and viewDidUnload lifecycle methods. (Note: viewDidUnload was deprecated and shouldn't be implemented in iOS6+; see iOS 6 - viewDidUnload migrate to didReceiveMemoryWarning?)

An important note:

viewDidUnload is not guaranteed to be called - it's not a standard lifecycle method.

From Apple doc:

viewDidUnload When a low-memory condition occurs and the current view controller’s views are not needed, the system may opt to remove those views from memory. This method is called after the view controller’s view has been released and is your chance to perform any final cleanup.

Instead, dealloc is called whenever the number of references for that receiver is zero.

Hope it helps.

Edit

For the sake of completeness you can see this link on how to avoid-nsnotification-removeobserver. The link provide some useful guidelines to remove observer (see also the comments). The author does it in viewDidAppear/viewDidDisappear methods since viewWillAppear and viewWillDisappear are not always called correctly in many applications. It's your choice.

If you want to be sure to remove observers in the right way unregister it in dealloc method or when the view is fully unloaded as you wrote in the second comment. But be sure that dealloc will be call in the future. In other words, as I already mentioned, if the controller continues to stay alive since some other object has a referenced to it, the method will never get called. In this case the controller continues to receive notifications.

Parent answered 26/4, 2012 at 13:45 Comment(7)
Thanks. But as you mention; 'viewDidUnload is not called before dealloc method. Sometimes a VC's dealloc will be called without viewDidUnload being called and, in this situation, using your suggestion means the VC is left as an observer after it is deallocated as viewWillDisappear / viewDidunload will never be called. Or is viewWillDisappear absolutely guaranteed to be called before dealloc?Leilanileininger
@1ndivisible viewWillDisappear is called before the view disappears from the screen. It means that when you remove the view's controller from the screen (e.g. the controller is popped from a navigation controller) that method is called. I added an edit for you.Parent
Nice. The avoid-nsnotification-removeobserver link is a nice one. Had no idea.Leilanileininger
You should be sure that dealloc is called irrespective of whether you're using notifications or not! Given that if your program is working correctly then it will certainly be called when the object is being released, dealloc is the best place for unregistering, which makes init (or a variant thereof) the only place for registering.Risarise
@mcmahon yes you are right. I added an edit for this. Thanks for your comment.Parent
I still don't fully understand why, but adding and removing an observer in viewWillAppear/Disappear would sometimes crash my app. Moving them into viewDidAppear/Disappear fixed it for me.Crissy
It's worth noting that viewDidUnload is deprecated and not called since iOS6! I've edited the answer to add a note about this.Moskva
C
2
- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   [[NSNotificationCenter defaultCenter] addObserver:self .........]
}

- (void)viewWillDisappear:(BOOL)animated
{
   [super viewWillDisappear:animated];
   [[NSNotificationCenter defaultCenter] removeObserver:self .........];
}
Chimpanzee answered 23/12, 2013 at 7:19 Comment(0)
V
2

For folks stumbling on this page more recently, the removal of observers might not be necessary anymore. The "Discussion" section of the addObserver(_:selector:name:object:) docs say:

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method. Otherwise, you should call removeObserver(_:name:object:) before observer or any object passed to this method is deallocated.

Vanquish answered 6/2, 2018 at 4:33 Comment(1)
Well thanks for that. I was having an issue where my viewWillDisappear was being called without viewWillAppear, and it was crashing.Refill
F
1

Why would you not do it in viewWillAppear / viewDidDisappear? You only care about the notifications when your view is showing anyway, right?

Filiate answered 26/4, 2012 at 13:25 Comment(5)
But as far as I'm aware there is no guarantee that viewDidDisappear is called if the object is deallocated. If it isn't, the object is left as an observer after it is deallocated.Leilanileininger
Apparently they will always be called. (See above)Leilanileininger
No in certain applications one cares about when the view is not showing either i.e update data stateMisuse
If the address book is updated while the app is in the background or even if my app is in another view I still want a notification. Thus viewDidDisappear is not suitable.Gliadin
No I also care about a view that is covered up by another viewGoggle
H
0

You can addObserver in viewWillAppear,and removeObserver in viewWillDisappear. but viewWillAppear may called many times. so you can remove Notification first then addObserver.

 -(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillShowNotification" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillHideNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)name:@"UIKeyboardWillShowNotification"object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)name:@"UIKeyboardWillHideNotification"object:nil];
 }

 -(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillShowNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIKeyboardWillHideNotification" object:nil];
 }
Hiphuggers answered 16/9, 2015 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.