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?
There are a lot of discussions about removing notifications in the right way. For example:
I suggest you to remove observers in
viewWillDisappear
(orviewDidDisappear
) andviewDidUnload
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:
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 sinceviewWillAppear
andviewWillDisappear
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 thatdealloc
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.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:You can addObserver in viewWillAppear,and removeObserver in viewWillDisappear. but viewWillAppear may called many times. so you can remove Notification first then addObserver.
Why would you not do it in
viewWillAppear
/viewDidDisappear
? You only care about the notifications when your view is showing anyway, right?