Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I remove a notification observer?

I set up a notification observer in my view controller's init method like so:

[[NSNotificationCenter defaultCenter] 
                    addObserver:self
                    selector:@selector(saveState)
                    name:UIApplicationWillResignActiveNotification
                    object:nil];

Where is the best place to call removeObserver:name:object: for this notification. I'm currently calling it in my dealloc method, but wanted to know if that might cause problems.

like image 785
nevan king Avatar asked Jan 13 '11 21:01

nevan king


2 Answers

No, you got it right. dealloc is the correct location to remove notification observers (unless you have some specific reason to need to remove the observer earlier).

like image 76
Lily Ballard Avatar answered Sep 22 '22 22:09

Lily Ballard


You can always remove the observer in viewWillDisappear:, or when you are done using it and have no other need for it, you can place it in a function.

like image 41
WrightsCS Avatar answered Sep 25 '22 22:09

WrightsCS