7

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

I want to 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.

Here is the scenario:

(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?

Sailesh
  • 25,517
  • 4
  • 34
  • 47

3 Answers3

3

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???
~~~~~~~~~~~~~~~~~~~~~Edited~~~~~~~~~~~~~~~~~~~~~~~~
hey I got some thing from here. 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.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • 2
    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 Jan 31 '12 at 10:18
  • hmmm that's a serious stuff... let me search if I can find something..thanks for replying :) – Inder Kumar Rathore Jan 31 '12 at 10:24
  • 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 Jan 31 '12 at 10:44
  • The main problem with this solution actually is that every observed will have to have a variable solely for this purpose - `alive` in this case. – Sailesh Jan 31 '12 at 10:46
2

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.

Joris Kluivers
  • 11,894
  • 2
  • 48
  • 47
  • 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 Jan 31 '12 at 10:21
  • 1
    In my opinion two objects observing each other is a case of bad design. – Joris Kluivers Jan 31 '12 at 10:41
  • 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 Dec 28 '14 at 19:54
1

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

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • 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 Jan 31 '12 at 09:18