David
David

Reputation: 3323

NSNotificationCenter Standard Practice?

Is is standard to balance the 'addObserver' with:

[[NSNotificationCenter defaultCenter] removeObserver:self]

in the viewWillDisappear method?

For instance, when registering to receive the results of NSURLConnection events by a Data Manager class, triggered by different tabs of a TabBar (separate controllers). Each controller registers to be 'notified' of receiving results in viewDidLoad. Should each controller also unregister? It seems that if the name: of the notification is the same for all, it might cause a disaster when tab's view reappears ??

Upvotes: 1

Views: 260

Answers (1)

Matt Hudson
Matt Hudson

Reputation: 7348

Every addObserver should have a remove observer. You should be very careful not to addObservers when an observer is already added, for instance in viewDidAppear observers can be added many times and then your observing selector will get called many times.

Honestly, from the example you explain you should consider a delegate protocol. See that here:

http://mobiledevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

Upvotes: 4

Related Questions