

Simple iOS Key Value Observing (KVO) Explained - gherlein
http://blog.herlein.com/2012/01/simple-ios-key-value-observing-kvo-explained/

======
LinaLauneBaer
This explanation is good to start with but the reader has to read a much more
in depth explanation afterwards because the example given by the author is
flawed:

He is registering an observer without using a context. If you remove the
observer without specifying a context then bad things can happen if two
observers do the same. For this reason there is a new method for this in 10.7
and this fact should have been mentioned because so many people have wasted so
many hours debugging KVO.

~~~
pothibo
I have used KVO for quite some times and I've never had to add a context to
it. This could be because I don't see the benefit of adding such context.

Would you have any example to show the use of context? I've always wondered
why I should use context so this would not only educate me but others that are
like me: clueless about context ;)

~~~
andrewsardone
“…by specifying a context every time and using one that’s not only unique to
your object but also to the file containing your subclass you can ensure that
the message sent to you is actually for you and you avoid potentially stepping
on the toes of your super class.”

<http://zearfoss.wordpress.com/2011/11/29/a-cool-kvo-trick/>

~~~
pothibo
I get it now.

You also could use [object class] instead of an arbitrary static pointer.

I never had issue with a class and a subclass using the same observer keypath.
Whenever I use it I always call super to make sure everything underneath is
called. But that's a good thing to know for sure, thanks.

------
liquidchaz
Another tip I would add is that messages sent to
observeValueForKeyPath:ofObject:change:context: may not occur on the main
thread.

For example, if you have multiple operations in an NSOperationQueue (like web
service requests), and one of those operations updates a property on the
object being observed, the observing method is called on that operation's
thread. This can cause issues if the observing method needs to update the GUI.
In this case, you'd need to use GCD to make sure the contents of the listening
method execute on main thread (or alternatively, make sure the object's
property is updated on the main thread).

------
chriseidhof
It's a very minimal explanation. But it's good that more people know about
KVO, it can really simplify your code. It becomes even nicer when you use
something like BlocksKit, because then you don't have to do the annoying
checks in the observer method:
[http://dizzytechnology.com/data/BlocksKit/Categories/NSObjec...](http://dizzytechnology.com/data/BlocksKit/Categories/NSObject+BlockObservation.html)

------
gherlein
Thanks for the elaborations. It sounds like 10.7/iOS5 solves some of the
issues brought up at <a href="[http://www.mikeash.com/pyblog/key-value-
observing-done-right...](http://www.mikeash.com/pyblog/key-value-observing-
done-right.html>here</a>).

