Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the better way to addObserver/removeObserver with NSNotificationCenter?

I used to addObserver in viewDidLoad: and removeObserver in dealloc:. Code:

- (void)viewDidLoad {     [super viewDidLoad];      [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(refreshData)                                                  name:AnyNotification                                                object:nil]; }  - (void)dealloc {     [[NSNotificationCenter defaultCenter] removeObserver:self                                                     name:AnyNotification                                                   object:nil]; } 

But according to some articles said, it's better to addObserver in viewDidAppear: and removeObserver in viewDidDisappear:. Code:

- (void)viewDidAppear:(BOOL)animated {     [super viewDidAppear:animated];      [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(refreshData)                                                  name:AnyNotification                                                object:nil]; }  - (void)viewDidDisappear:(BOOL)animated {     [super viewDidDisappear:animated];      [[NSNotificationCenter defaultCenter] removeObserver:self                                                     name:AnyNotification                                                   object:nil]; } 

So, what's the better way to addObserver/removeObserver?

like image 824
fannheyward Avatar asked May 17 '12 07:05

fannheyward


People also ask

Should I remove notification observer?

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.

How do I delete an observer in Objective C?

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.

Do you need to remove observers Swift?

As of iOS 9 (and OS X 10.11), you don't need to remove observers yourself, if you're not using block based observers though. The system will do it for you, since it uses zeroing-weak references for observers, where it can.


1 Answers

this depends on your scenario, usually the best approach is to add in viewDidLoad and remove in dealloc and in viewDidUnload (deprecated in iOS 9.0, use dealloc only), but there are some cases when you have same method in different classes like UI effects and want to call only current screen's method using notification, then you will have to add the observer in viewWillAppear and remove it in viewWillDisappear or viewDidAppear/viewDidDisappear

Edit: A note from comments, thanks @honey.

Though now since iOS 9, you no longer need to care about removing the observer. See Apple release notes: "In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated..

like image 70
Saad Avatar answered Sep 21 '22 18:09

Saad