Image:CocoaNav icon.png

PARMANOIR

Key Value Bastard Observing

Key Value Bastard Observing (KVBO) is a technique to observe changes to all keys of all instances of a class. If you've ever written code to observe a collection, adding observers to newly created objects, removing deleted object's observers, adding another observer for that one more key you need to check, … KVBO will save you time and aspirin.

Making a class KVBO compliant

We'll make a class called SampleObject KVBO compliant.

  • Overload setValue:forKey: to listen to all changes
  • Save self and the key name (from forKey:) with a class method called setLastModifiedKey:forInstance:. They will be stored as static variables and will be read by KVBO observers
  • setValue:forKey: then sets a new value for a dummy key (keyChanged) on a shared instance (a singleton for the observed class, here SampleObject). Changing this dummy key's value dispatches KVO notifications of our shared instance

From here, an observer listening to the shared instance will receive notifications of all changes to all instances of SampleObject and be able to see which key of which instance changed.

Listening to KVBO

You can listen using 2 methods :

  • standard KVO : use addObserver:forKeyPath:options:context: to listen to your KVBO class' shared instance. All notifications will go through the KVO method observeValueForKeyPath:ofObject:change:context:
  • bindings : use bind:toObject:withKeyPath:options: to bind the shared instance to an observer. All notifications will go through the set method of the key you bind to

Which one to use ? That's a matter of taste. Using standard KVO, you have a fixed name method (addObserver:forKeyPath:options:context:) to receive all notifications. Using bindings, you get to choose the name of your method, but it has to start with set.

Sample code

Image:iconZip.png KeyValueBastardObserving.zip

2008 07 25Where in build phases is that file ?
2008 07 192Imagine clickable error messages
2008 07 18Succulent Stormhoek
2008 07 173Loving the for
2008 07 12(Parmanoir) Feed now validates
2008 07 108Telling classes from instances
2008 07 08Comma Trick
2008 07 06Using libffi
2008 07 04BridgeSupport's type and type64
2008 07 042Clickable Disabled MenuItems
2008 07 026Less bugs through compiler optimizations
2008 06 251CocoaNav JS, a light CocoaNav for Safari
2008 06 232NSWindow goodies : bottomCornerRounded, usesLightBottomGradient
2008 06 222Inspecting NSUndoManager's undo stack
2008 06 16Cocoa Regular Expressions via JavascriptCore
2008 06 15Crossing the WebKit bridge
2008 06 08Double and Triple Click
2008 06 05Photoshop-like compositing with Core Animation
2008 06 052One way binding to NSSlider
2008 05 30Threaded Core Animation, Part Deux
Image:rss.png
Image:rss.png

Powered by MediaWiki

Hi ! I'm learning Cocoa to (hopefully !) become an indie developer.

I've written software all my professional life, in C++, PHP, Javascript. I've designed websites and web interfaces. My last venture went into flames as clients were happy but didn't like paying very much.

I've had little luck in the B2B world, I'm hoping for a better future writing Mac applications.

Planet Cocoa