Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 817
tangqiaoboy Avatar asked Feb 21 '23 00:02

tangqiaoboy


1 Answers

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.

like image 101
newton_guima Avatar answered Apr 28 '23 16:04

newton_guima