5

In a controller we have added observer in viewDidAppear and it's removed in viewWillDisappear. There is no observer in init/viewDidLoad.

In such a case the below line for safety purpose/is it required in dealloc method?

[[NSNotificationCenter defaultCenter] removeObserver:self];

Now the question/doubt is are there scenarios where dealloc will get called without viewWillDisappear not being called? What about when memory warnings are called. What happens in those cases?

Thanks.

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
selva
  • 799
  • 2
  • 13
  • 25
  • i'm so far happy in my code that viewwilldisappear will be called but i will await the answer. One scenario brought up in discussion was it was suggested the view may not disappear on a memory warning. If this happened would the code run properly in viewdiddisappaer even if called later when the view did disappear. – LanternMike Apr 30 '14 at 07:35

1 Answers1

4

One scenario is:

  1. VC1 implements the class HelperVC, it's delegates and added HelperVC as it's subview.
  2. Close button on the HelperVC calls closeAll delegate method, which is implemented in VC1.
  3. The closeAll method in VC1 sets the HelperVC object to nil.
  4. Now dealloc method is called in HelperVC instead of viewWillDisappear. Because we didn’t remove the HelperVC view, we have made HelperVC object to nil.
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Manoj
  • 1,482
  • 2
  • 20
  • 53