Pierre Bernard

My little hideout on the net

December 21
November 21
October 21
September 21
August 21
July 21
June 21
May 21
April 21
March 21
February 21
January 21
December 20
November 20
October 20
September 20
August 20
July 20
June 20
May 20
April 20
March 20
February 20
January 20
December 19
November 19
October 19
September 19
August 19
July 19
June 19
May 19
April 19
March 19
February 19
January 19
December 18
November 18
October 18
September 18
August 18
July 18
June 18
May 18
April 18
March 18
February 18
January 18
December 17
November 17
October 17
September 17
August 17
July 17
June 17
May 17
April 17
March 17
February 17
January 17
December 16
November 16
October 16
September 16
August 16
July 16
June 16
May 16
April 16
March 16
February 16
January 16
December 15
November 15
October 15
September 15
August 15
July 15
June 15
May 15
April 15
March 15
February 15
January 15
December 14
November 14
October 14
September 14
August 14
July 14
June 14
May 14
April 14
March 14
February 14
January 14
December 13
November 13
October 13
September 13
August 13
July 13
June 13
May 13
April 13
March 13
February 13
January 13
December 12
November 12
October 12
September 12
August 12
July 12
June 12
May 12
April 12
March 12
February 12
January 12
December 11
November 11
October 11
September 11
August 11
July 11
June 11
May 11
January 10
December 09
November 09
October 09
September 09
August 09
May 09
April 09
March 09
February 09
January 09
November 08
October 08
September 08
June 08
May 08
April 08
March 08
February 08
January 08
December 07
November 07
October 07

Key-Value Observing Done Right. Again.

I love using KVO for all sorts of things: side-effect, flagging UI as dirty, resetting caches, …

Writing the observeValueForKeyPath:ofObject:change:context: method has long since become tedious. So I created a TextExpander snippet to write it for me.

Still, I don't quite like it relying of string comparisons (which I use for context). And I regularly find myself forgetting to unregister observers.

Mike Ash wrote a interesting blog post where he makes a couple of good points against the current KVO API. The most important of which, I think, is the fact unregistering an observer might actually break behavior upon which the superclass relies.

Andy Matuschak has proposed a very convenient API which uses blocks to handle observation callbacks. While I love this solution, I found it to still have a couple of drawbacks:

  • Andy's KVO+Blocks requires Mac OS X Snow Leopard. It relies not only on blocks, but also on associated objects and Grand Central Dispatch.
  • Code is not always easer to read when the callback block is written where it is registered. At times, I would prefer a traget+action setup
  • KVO+Blocks make it all too easy to create retain cycles: referencing an instance variable from within the block retains the owning object.

Inspired by Mike's and Andy's writings and work, I have come up with my own implementation: HHKeyValueObserver.

  • Works on both Leopard and Snow Leopard. Relies on HHAssociatedObjects
  • Target action mechanism with the following signatures: actionWithInfo:change: or actionWithInfo:
  • Uses blocks where available
  • Unregisters automatically on dealloc of the observer

Retain cycles may be avoided by referring to the observed object by the way of observationInfo.observee.

Comments