KVO and ARC how to removeObserver KVO and ARC how to removeObserver ios ios

KVO and ARC how to removeObserver


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.


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}}    


Elsewhere on stack overflow, Chris Hanson advises using the finalize method for this purpose and implementing a separate invalidate method so that owners can tell objects that they are done. In the past I have found Hanson's solutions to be well-thought-out, so I'll be going with that.