Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeObserver:forKeyPath: in dealloc

If my instance is observing some property of another object, am I supposed to call removeObserver:forKeyPath: in dealloc?

like image 445
cfischer Avatar asked Oct 19 '11 19:10

cfischer


2 Answers

Yes

...unless there is a more appropriate time earlier in execution.

Observers are held as nonretained references. Failing to un-register can result in your deallocated instance being messaged.

like image 118
justin Avatar answered Nov 09 '22 23:11

justin


You need to use -removeObserver:forKeyPath: to remove the observer before -[NSObject dealloc] runs, so yes, doing it in the -dealloc method of your class would work.
refer this questions.
Removing an Observer
question2

like image 29
Parag Bafna Avatar answered Nov 10 '22 00:11

Parag Bafna