Search code examples
iphoneiosipadnsnotification

Should I unregister NSNotification both in viewDidUnload and dealloc?


I register a NSNotification in viewDidLoad method.

Should I unregister it both in viewDidUnload and dealloc method using the code below?

[[NSNotificationCenter defaultCenter] removeObserver:self];

Thanks.


Solution

  • Yes you should. viewDidUnload is not called when the view controller is deallocated.

    Because viewDidLoad is called when the view controller is opened, people sometimes mistakenly assume that its opposite (viewDidUnload) is called when the screen closes. That is not the case, viewDidUnload is only used in low-memory situations.

    That’s why we need to unregister for the notifications in dealloc as well.