-1

I every time use dealloc for remove observer, but just faced with this link that describes that we can use viewWillDisapper instead of dealloc.

Community
  • 1
  • 1
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

3 Answers3

3

The choice is a matter of properly pairing the addObserver/removeObserver calls.

If you call addObserver in a place that is called once such as some init method or viewDidLoad then call removeObserver in dealloc.

If you call addObserver in a place that is called multiple times like viewWillAppear then call removeObserver in viewWillDisappear.

It's the proper pairing that matters.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • thank you for answer I know how the observers and how they work and it depends on the logic that I need, but I think using it in the viewWillDisappear is not good because of the memory usage, also I can add for example IBAction that will hide for example view and then remove observer also =) or any other crazy idea) – Matrosov Oleksandr Apr 22 '14 at 16:31
  • I don't know why you think there is a memory issue removing an observer in `viewWillDisappear`. As I stated, only call `removeObserver` from `viewWillDisappear` if you call `addObserver` from `viewWillAppear`. If that doesn't meet your needs then don't setup/remove the observer in those two methods. Use whichever pair make sense for your need. – rmaddy Apr 22 '14 at 16:37
  • so, never mind ) I just to wanted to hear suggestion and all answers are good. I think I have confused. – Matrosov Oleksandr Apr 22 '14 at 16:41
  • I just edited question with removing "pretty sure" line ) thank you – Matrosov Oleksandr Apr 22 '14 at 16:45
2

One critical thing about KVO is that you MUST match removeObserve and addObserver calls and you cannot add duplicate observers. This means that you must carefully think through where you add the observer and where you remove it so that you don't violate either of those restrictions.

If you add it in viewDidLoad it is currently sufficient to remove it in dealloc (since viewDidUnload isn't used any more), but the observer may trigger when the view isn't visible. If you're running on older OS'es where viewDidUnload is still called, this can also be problematic as you have to track when the observers are in place and when they aren't.

You can add it in viewDid/WillAppear, in which case you need to remove it in viewDid/WillDisappear. This is usually cleaner since the calls are (generally speaking) guaranteed to be matched up.

David Berry
  • 40,941
  • 12
  • 84
  • 95
2

The link you reference is relatively informative and accurate.

I think of it in these terms:

a) does the VC need to be notified while it's on screen only? use viewWillAppear/viewWillDisappear.

b)does the VC need to be notified as long as it's alive (but not necessarily on screen)? use init or viewDidLoad and remove in dealloc.

I've breakpointed my dealloc methods under ARC and they're called when expected. However viewDidUnload is not called.

GnarlyDog
  • 1,247
  • 1
  • 17
  • 24
  • yes I got the idea, honestly I've never faced with situation when view is off and controller is still in the memory. I think it is not good for memory usage if we need to have some controller of some view that we don't use. Usually when the view is off the controller is dropped as well, because everything I need is in the data model and I can restore it again when I need it. So maybe it means if we delete observer in viewWillDisappear then we have inaccurate logic of usage our memory. – Matrosov Oleksandr Apr 22 '14 at 16:26
  • 1
    @MatrosovAlexander, if you do any pushes or modal presentations, then you will have controllers that are still in memory (the one you pushed from or the presenting view controller) but their views will be off screen -- this is actually a very common situation. – rdelmar Apr 22 '14 at 16:30
  • yes I understood, but it is not good any case, if I will push some controllers many time it will cause my app to crash I think – Matrosov Oleksandr Apr 22 '14 at 16:34
  • so I think in any case the answer has right logic because of responsibility each programers how they make architecture. So and it should depend on the logic that you need instead of rules for example as in case with dealloc – Matrosov Oleksandr Apr 22 '14 at 16:39
  • so, never mind ) I just to wanted to hear suggestion and all answers are good. I think I have confused. – Matrosov Oleksandr Apr 22 '14 at 16:42
  • UITabBarController is another example of view controllers that can be loaded but not on screen. Once you select a tab, that tab's view controller's view will load, and it will stay loaded even when you switch to another tab. It's possible (depending on requirements) that the view controller only be notified of updates if it's actually front and center. – GnarlyDog Apr 22 '14 at 20:39