background preloader

KVO

Facebook Twitter

NSKeyValueObserving Protocol Reference. (informal protocol) Overview The NSKeyValueObserving (KVO) informal protocol defines a mechanism that allows objects to be notified of changes to the specified properties of other objects.

NSKeyValueObserving Protocol Reference

You can observe any object properties including simple attributes, to-one relationships, and to-many relationships. Observers of to-many relationships are informed of the type of change made — as well as which objects are involved in the change. NSObject provides an implementation of the NSKeyValueObserving protocol that provides an automatic observing capability for all objects. Class Methods automaticallyNotifiesObserversForKey: Returns a Boolean value that indicates whether the receiver supports automatic key-value observation for the given key. + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key Discussion The default implementation returns YES. Availability Available in OS X v10.3 and later. Declared In keyPathsForValuesAffectingValueForKey: Parameters key Available in OS X v10.5 and later. change object.

Back to Basics: Using KVO. One of the things I like most about Apple’s iOS SDK is the consistent and easy-to-use API they provide.

Back to Basics: Using KVO

Across all their different frameworks there’s a pattern at work that makes using their classes easy to understand. This is due in part to the simplicity for configuring those objects. In most cases you don’t need to call cryptic methods to setup or teardown classes. If you want to change a label’s font, you just set a property. If you want to add a new set of tabs to a UITabBarController, you simply have to assign an array of view controllers to the “viewControllers” property and away you go. What is Key-Value-Observing? Key-Value-Observing, or more commonly KVO, is a mechanism by which you can observe changes to keys and their values bound to an object. You might say “Really? Using KVO in a class essentially buys you flexibility and easy-to-read code, and with a few general practices can be made easy to read and easy to extend.

Setting up KVO the hard way Observing Options. KVC/KVO and Bindings. How to check if an object is an observer? Key-Value Observing Done Right. Key-Value Observing Done Right Cocoa's Key-Value Observing facilities are extremely powerful and useful.

Key-Value Observing Done Right

Unfortunately they have a really terrible API that's inherently broken in a couple of different ways. I want to discuss how it's broken, and a way to make it better. What's Broken There are three major problems with the KVO API, all of which relate to multiple levels of the class hierarchy registering observers. This is important because even NSObject (as part of its implementation of -bind:toObject:withKeyPath:options:) will observe things.

-addObserver:forKeyPath:options:context: doesn't allow passing a custom selector to be invoked. So what can we do about it? Svn co And you can browse it by just clicking the link above. So how does it work? MAKVONotificationCenter then bypasses all three deficiencies described above: A custom selector is provided in the -addObserver:... method which is invoked when the observed key path changes. There are some interesting features to note. Comments: KVO guide. Key-value observing is a mechanism that allows objects to be notified of changes to specified properties of other objects.

KVO guide

Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects. It is particularly useful for communication between model and controller layers in an application. (In OS X, the controller layer binding technology relies heavily on key-value observing.) A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller. Key-Value Coding Programming Guide.