KVO - How to check if an object is an observer? KVO - How to check if an object is an observer? ios ios

KVO - How to check if an object is an observer?


[...] is it possible to check if an object actually is observing that property?

No. When dealing with KVO you should always have the following model in mind:

When establishing an observation you are responsible for removing that exact observation. An observation is identified by its context—therefore, the context has to be unique. When receiving notifications (and, in Lion, when removing the observer) you should always test for the context, not the path.

The best practice for handling observed objects is, to remove and establish the observation in the setter of the observed object:

static int fooObservanceContext;- (void)setFoo:(Foo *)foo{    [_foo removeObserver:self forKeyPath:@"bar" context:&fooObservanceContext];    _foo = foo; // or whatever ownership handling is needed.    [foo addObserver:self forKeyPath:@"bar" options:0 context:&fooObservanceContext];}- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{    if (context == &fooObservanceContext) {        // handle change    } else {        // not my observer callback        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];    }}- (void)dealloc{    self.foo = nil; // removes observer}

When using KVO you have to make sure that both objects, observer and observee, are alive as long as the observation is in place.

When adding an observation you have to balance this with exactly one removal of the same observation. Don't assume, you're the only one using KVO. Framework classes might use KVO for their own purposes, so always check for the context in the callback.

One final issue I'd like to point out: The observed property has to be KVO compliant. You can't just observe anything.


Part of the NSKeyValueObserving protocol is this:

 - (void *)observationInfo

which should list the observers.

EDIT Useful for debugging only.


I underestand this an objective-c question. But since lots of people use Swift/objective-c together, I thought I point out the advantage of the Swift4 new API over older versions of KVO:

If you do addObserver multiple times for KVO, then for each change you’ll get the observeValue as many as the current number of times you’ve added yourself as the observer.

  • And to remove yourself you have to call removeObserver as many times as you added.
  • Removing it more than you’ve added will result in a crash

The Swift4 observe is far smarter and swiftier!

  • If you do it multiple times, it doesn’t care. It won’t give multiple callbacks for each change.
  • And only one invalidate of the token is enough.
  • invalidating it before beginning to observer or more times that that you’ve done observe will not result in a crash

So to specifically answer your question, if you use the new Swift4 KVO, you don't need to care about it. Just call invalidate and you're good. But if you're using the older API then refer to Nikolai's answer