Search code examples
iphoneobjective-cioskey-value-observingkey-value-coding

Helper classes/libraries to make use of KVC/KVO on iOS more practical/safe?


This question is really looking for a specific class that I know exists, but cannot find.

A few weeks ago I read through an article outlining a helper class for using KVC - it let you encapsulate KVC subscriptions so that you could have multiple values being observed without having to go through the same hander method, and also made cleanup of the KVC observers safer (since KVC cleanup is really finicky and exception prone).

So, in responses I am looking for either that helper class I had run across before, or other examples of attempts to wrap KVC to make it safer and more palatable for general use.


Solution

  • I finally found what I was looking for, the key to searching was to look for examples of using KVO with blocks.

    I found three interesting resources, which I'll present as a starting point - if anyone has others that are good please also answer.

    The first link is the article I mentioned finding before. It has a great summary of the problems with KVO as it stands, and an interesting helper class that encapsulates a KVO session:

    http://www.mikeash.com/pyblog/key-value-observing-done-right.html

    The second is a simpler NSObject category that lets you pass in a block to be activated on a change notification:

    http://blog.andymatuschak.org/post/156229939/kvo-blocks-block-callbacks-for-cocoa-observers

    The last is another category on NSObject that uses a binding mechanism to add observation:

    http://wirestorm.net/blog/2011/05/27/block-based-observation-with-kvo/