Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding and removing observers to NSNotificationCenter in a UIViewController

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?

like image 633
Undistraction Avatar asked Apr 26 '12 12:04

Undistraction


People also ask

When to remove an observer from nsnotificationcenter?

One final requirement for working with NSNotificationCenter is to remove an observer when it no longer needs to listen for notifications. When might this situation arise? When the observer is no longer referenced and thus deallocated from memory, it should tell the default notification center that it can be removed

What is nsnotificationcenter in iOS?

However, NSNotificationCenter has nothing to do with sending or receiving push notifications in iOS. Rather, it is a communication tool internal to your app. It provides a way for one instance of a class/struct to notify one or more other class/struct instances about something.

Is nsnotificationcenter “push notifications”?

When I first heard NSNotificationCenter, I thought “push notifications”. However, NSNotificationCenter has nothing to do with sending or receiving push notifications in iOS. Rather, it is a communication tool internal to your app.

What is the difference between nsdistributednotificationcenter and a Notification Center?

A notification center can deliver notifications only within a single program; if you want to post a notification to other processes or receive notifications from other processes, use NSDistributedNotificationCenter instead.


2 Answers

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

  • removeobserver-with-nsnotification-what-am-i-doing-wrong
  • removing-a-nsnotificationcenter-observer-in-ios-5-arc

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.

like image 156
Lorenzo B Avatar answered Oct 31 '22 00:10

Lorenzo B


- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   [[NSNotificationCenter defaultCenter] addObserver:self .........]
}

- (void)viewWillDisappear:(BOOL)animated
{
   [super viewWillDisappear:animated];
   [[NSNotificationCenter defaultCenter] removeObserver:self .........];
}
like image 40
Darshit Shah Avatar answered Oct 30 '22 23:10

Darshit Shah