Should I unregister NSNotification in both the DidUnload view and dealloc?

I am registering NSNotification in viewDidLoad .

Should I cancel it in both the viewDidUnload and dealloc methods using the following code?

 [[NSNotificationCenter defaultCenter] removeObserver:self]; 

Thanks.

+4
source share
1 answer

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

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

This is why we need to unregister for dealloc notifications.

+8
source

Source: https://habr.com/ru/post/1403890/


All Articles