Valor clave Observar removeObserver cuando el objeto observado se desasigna

There are various questions related to this topic but none of them answer my question.

Quiero removeObserver from an object when that object gets deallocated. I am using KVO since multiple items need to be observed, and NSNotification has huge overhead in that case.

Aquí está el escenario:

(Multiple) Objects are being observed by various other objects. When the observer gets deallocated, I can remove it as an observer. But when the observed get deallocated, I need to tell all the observers to remove themselves as observers. How to do this?

preguntado el 31 de enero de 12 a las 08:01

3 Respuestas

Sailesh if the object (observed) get deallocated, then that object can't be altered or it's value cant be changed so if your observers are observing a deallocated object and that deallocated object is never going to send any kind of observation notification then Is there any fun of removing observers???
hey I got some thing from aquí. The idea is this you observe an additional property say alive and write in dealloc of observed as alive = NO; and as this property is changed all observer will get notified and thus you can remove all observer.


contestado el 23 de mayo de 17 a las 15:05

After seeing this error message in console, you'd like to removeObserver, even though its no fun :). An instance 0xd89f200 of class MyClass was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. - Sailesh

hmmm that's a serious stuff... let me search if I can find something..thanks for replying :) - Inder Kumar Rathore

Thanks for the answer. I saw that answer, but I was looking for a more cleaner solution. In this case, every observer needs to observe additional keypath. Is there no way that the observer get to know that the observed is getting deallocated, without the observed getting to know anything? I know thats a long shot, but would be really cool if its possible. - Sailesh

The main problem with this solution actually is that every observed will have to have a variable solely for this purpose - alive en este caso. - Sailesh

I think there is a design issue here. The observed object should not have to care about it's observers. You say you want to remove the observers from the observed objects dealloc method. But why does it get deallocated? If it's still observed there is an ownership somewhere thus the object will not be dealloced. Good design results in no observers left by the time dealloc is called.

Imagine a view that registers itself as an observer to a model object. This model object is either retained in the view, or in the controller. The model object will not call it's dealloc as long as it's retained somewhere. Only when the last ownership is released, it should call dealloc. So say all ownerships are released, except for the view/viewcontroller and there's one observer left (the view). Now before the view/viewcontroller releases its ownership over the model object, it should also remove the view as an observer. So by the time the dealloc method is called, there should be no observer left.

Respondido el 31 de enero de 12 a las 13:01

Consider a case when two objects observe each other. The design that you suggest would create a retain cycle. In any case, the observer may only have a weak reference to the observed, and doing otherwise may not be good design, considering all other aspects of the app. - Sailesh

In my opinion two objects observing each other is a case of bad design. - Joris Kluivers

Thanks for this answer. I realised that my decision of having a weak ownership over the model was wrong. I switched to a strong relationship now, and hopefully, I don't see a lot of KVO crashes now. - rounak

you can separately post a notification from -(void)dealloc when observed get deallocated. on receiving all other observers will remove the required notification.

Respondido el 31 de enero de 12 a las 13:01

Won't there be so many notifications, and the whole purpose of using kvo gets defeated. Also, it would end up like: 1. Observe some property of an object. 2. From all the observed objects, fire a notification when deallocated. 3. Listen to the deallocation notification and remove observer. Thats a lot of work. Isn't there some clean solution? - Sailesh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.