removeObserver:forKeyPath: in dealloc
Asked Answered
B

2

5

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

Bundesrat answered 19/10, 2011 at 19:4 Comment(0)
R
10

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.

Romola answered 19/10, 2011 at 19:20 Comment(0)
N
3

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

Nonparticipation answered 19/10, 2011 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.