12

If I add an observer to the [NSNotificationCenter defaultCenter] in my viewDidLoad should I be removing it in viewDidUnload?

Jason
  • 28,040
  • 10
  • 64
  • 64
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294

3 Answers3

11

If you need to add these in your initializer, you should remove it in the dealloc method. Ideally, you should only care about these notifications when you are currently onscreen or not.

The viewDid[Appear|Disappear] methods can be called multiple times during the lifetime of a UIViewController. Register for the notification in the viewDidAppear method and unregister it in viewDidDisappear.

Jason
  • 28,040
  • 10
  • 64
  • 64
  • What cases is viewDidUnload not called but dealloc is? – morningstar Sep 30 '11 at 17:17
  • When the controller is popped from a navigation stack, for example. viewDidUnload is really only called when low-memory conditions force the view to be unloaded even though the controller is still around. – BJ Homer Sep 30 '11 at 17:59
  • dealloc callback or its timing is not reliable...isn't that true...? – Moonwalker Dec 10 '12 at 16:50
  • 1
    viewDidUnload Deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called. Adding remove observers to multiple points is not clean code. Use dealloc. – Luke Mcneice Jun 12 '13 at 14:35
  • Answer was from 2011. I've edited answer to reflect deprecation. – Jason Jun 12 '13 at 15:12
  • The comment about "should only care about these notifications when you are currently onscreen or not" seems to be an overly simplistic statement. In an app with a tab bar, screens may be interested in processing state information (in response to notifications) even if a user has tapped another tab. – software evolved Jan 17 '14 at 01:45
8

You should remove it in dealloc method.

knuku
  • 6,082
  • 1
  • 35
  • 43
-1

It seems to me viewDidUnload is the place to put it.

If the notification handler that gets called accesses any of the views managed by the view controller, that will either be an error or will cause the view to get reloaded unnecessarily. If your view is not being shown, then most likely the view controller doesn't need to be notified. If it does, at least check if the view is loaded before you make any changes to it. While the view is not loaded, you might still need to update the state of your view controller, for example change or dirty cached values, but don't update the view until it loads again.

Two, what happens if you don't removeObserver in viewDidUnload, and viewDidLoad gets called again? You call addObserver again. Probably doesn't hurt, the notification center can detect duplicate adds.

morningstar
  • 8,952
  • 6
  • 31
  • 42
  • 1
    IIRC, if you call addObserver twice with the same notification you will get two callbacks. – Jason Sep 30 '11 at 18:13
  • 1
    viewDidUnload Deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called. – Luke Mcneice Jun 12 '13 at 14:34