KVO and ARC how to removeObserver

20,263

Solution 1

You still can implement -dealloc under ARC, which appears to be the appropriate place to remove the observation of key values. You just don't call [super dealloc] from within this method any more.

If you were overriding -release before, you were doing things the wrong way.

Solution 2

I do it with this code

- (void)dealloc
{
@try{
    [self.uAvatarImage removeObserver:self forKeyPath:@"image" context:nil];
} @catch(id anException) {
    //do nothing, obviously it wasn't attached because an exception was thrown
}
}    
Share:
20,263
drunknbass
Author by

drunknbass

I write code, lots of it.

Updated on March 26, 2020

Comments

  • drunknbass
    drunknbass over 4 years

    How do you remove an observer from an object under ARC? Do we just add the observer and forget about removing it? If we no longer manage memory manually where do we resign from observing?

    For example, on a view controller:

    [self.view addObserver:self
                forKeyPath:@"self.frame"
                   options:NSKeyValueObservingOptionNew 
                   context:nil];
    

    Previously, I would call removeObserver: in the view controller's dealloc method.

  • Elise van Looij
    Elise van Looij over 12 years
    Are you sure about this? I quote from clang.llvm.org/docs/…, section 7.1.2. dealloc: "Rationale: even though ARC destroys instance variables automatically, there are still legitimate reasons to write a dealloc method, such as freeing non-retainable resources. Failing to call [super dealloc] in such a method is nearly always a bug. "
  • Björn Landmesser
    Björn Landmesser over 12 years
    @ElisevanLooij Yes that's true. If you derive from this class, it seems obvious that you must call [super dealloc]. Who else should do this for you.
  • Björn Landmesser
    Björn Landmesser over 12 years
    @ElisevanLooij Oops, well, should have checked before. It is not allowed to call [super dealloc] in a dealloc method. No idea how this would work then when subclassing the mentioned class. Maybe it is just advisable to use finalize instead (where you call [super finalize])
  • Brad Larson
    Brad Larson over 12 years
    @ElisevanLooij - The point they were trying to make there is in regards to the manual memory management case. Because not calling [super dealloc] last in that method is pretty much always a bug under manual memory management, the compiler handles it for you now, which is why you can't call -dealloc directly anymore. The only things you put in a -dealloc method under ARC are any non-object resources you need to free, or cleanup tasks like removing observers. The wording they use is a little muddy, but this is what they meant.
  • Brad Larson
    Brad Larson over 12 years
    Note that he was referring to garbage collection there, not ARC (his answer was written in 2008). Under garbage collection, -dealloc is never called. In ARC, it is. It's perfectly acceptable to remove KVO observers in -dealloc, as Chris Lattner (who knows what he's talking about) indicates in Apple's developer forums here: devforums.apple.com/message/475850
  • Brad Larson
    Brad Larson over 12 years
    @BjörnMilcke - As I comment on Elise's answer, -finalize is used for this under garbage collection, where -dealloc is never called, but it's perfectly acceptable to place this code in -dealloc under ARC. [super dealloc] is called for you automatically, which is why it's an error to call it under ARC.
  • Elise van Looij
    Elise van Looij over 12 years
    Thanks Brad, for doing all this work. No to finalize, yes to dealloc but without [super dealloc]. Simple really, once you know it. Hey, @drunknbass, accept that man's answer!
  • Abizern
    Abizern over 10 years
    What is the point of Exception handling in dealloc? It's too late to do anything about it.
  • shoumikhin
    shoumikhin about 8 years
    What's the point of removing observers on an instance variable in dealloc? This uAvatarImage will be deallocated soon along with any observers it has subscribed to its key paths.
  • mac10688
    mac10688 over 7 years
    @shoumikhin I'm using ARC and I had to remove the observer in dealloc method. I have the same question you have. However, when I ran multiple instances of the class eventually I got the exc_bad_address error. Doing this resolved the issue. Also, the answer from here stackoverflow.com/questions/32490808/… helped me discover the issue.