Declared in NSObject+BKBlockObservation.h

Overview

Blocks wrapper for key-value observation.

In Mac OS X Panther, Apple introduced an API called “key-value observing.” It implements an observer pattern, where an object will notify observers of any changes in state.

NSNotification is a rudimentary form of this design style; KVO, however, allows for the observation of any change in key-value state. The API for key-value observation, however, is flawed, ugly, and lengthy.

Like most of the other block abilities in BlocksKit, observation saves and a bunch of code and a bunch of potential bugs.

Includes code by the following:

Tasks

Instance Methods

bk_addObserverForKeyPath:identifier:options:task:

- (void)bk_addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)token options:(NSKeyValueObservingOptions)options task:(void ( ^ ) ( id obj , NSDictionary *change ))task
Discussion

Adds an observer to an object conforming to NSKeyValueObserving.

Adds a block observer that executes the block upon a state change.

Parameters

keyPath

The property to observe, relative to the reciever.

token

An identifier for the observation block.

options

The NSKeyValueObservingOptions to use.

task

A block responding to the reciever and the KVO change. observation with removeObserverWithBlockToken:.

Declared In

NSObject+BKBlockObservation.h

bk_addObserverForKeyPath:options:task:

- (NSString *)bk_addObserverForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options task:(void ( ^ ) ( id obj , NSDictionary *change ))task
Discussion

Adds an observer to an object conforming to NSKeyValueObserving.

Adds a block observer that executes a block upon a state change with specific options.

Parameters

keyPath

The property to observe, relative to the reciever.

options

The NSKeyValueObservingOptions to use.

task

A block with no return argument and two parameters: the reciever and the change dictionary.

Return Value

Returns a globally unique process identifier for removing observation with removeObserverWithBlockToken:.

Declared In

NSObject+BKBlockObservation.h

bk_addObserverForKeyPath:task:

- (NSString *)bk_addObserverForKeyPath:(NSString *)keyPath task:(void ( ^ ) ( id target ))task
Discussion

Adds an observer to an object conforming to NSKeyValueObserving.

Adds a block observer that executes a block upon a state change.

Parameters

keyPath

The property to observe, relative to the reciever.

task

A block with no return argument, and a single parameter: the reciever.

Return Value

Returns a globally unique process identifier for removing observation with removeObserverWithBlockToken:.

Declared In

NSObject+BKBlockObservation.h

bk_addObserverForKeyPaths:identifier:options:task:

- (void)bk_addObserverForKeyPaths:(NSArray *)keyPaths identifier:(NSString *)token options:(NSKeyValueObservingOptions)options task:(void ( ^ ) ( id obj , NSString *keyPath , NSDictionary *change ))task
Discussion

Adds an observer to an object conforming to NSKeyValueObserving.

Adds a block observer that executes the same block upon multiple state changes.

Parameters

keyPaths

An array of properties to observe, relative to the reciever.

token

An identifier for the observation block.

options

The NSKeyValueObservingOptions to use.

task

A block responding to the reciever, the key path, and the KVO change. observation with removeObserversWithIdentifier:.

Declared In

NSObject+BKBlockObservation.h

bk_addObserverForKeyPaths:options:task:

- (NSString *)bk_addObserverForKeyPaths:(NSArray *)keyPaths options:(NSKeyValueObservingOptions)options task:(void ( ^ ) ( id obj , NSString *keyPath , NSDictionary *change ))task
Discussion

Adds an observer to an object conforming to NSKeyValueObserving.

Adds a block observer that executes the same block upon multiple state changes with specific options.

Parameters

keyPaths

An array of properties to observe, relative to the reciever.

options

The NSKeyValueObservingOptions to use.

task

A block with no return argument and three parameters: the reciever, the key path of the value change, and the change dictionary.

Return Value

A unique identifier for removing observation with removeObserverWithBlockToken:.

Declared In

NSObject+BKBlockObservation.h

bk_addObserverForKeyPaths:task:

- (NSString *)bk_addObserverForKeyPaths:(NSArray *)keyPaths task:(void ( ^ ) ( id obj , NSString *keyPath ))task
Discussion

Adds an observer to an object conforming to NSKeyValueObserving.

Adds a block observer that executes the same block upon multiple state changes.

Parameters

keyPaths

An array of properties to observe, relative to the reciever.

task

A block with no return argument and two parameters: the reciever and the key path of the value change.

Return Value

A unique identifier for removing observation with removeObserverWithBlockToken:.

Declared In

NSObject+BKBlockObservation.h

bk_removeAllBlockObservers

- (void)bk_removeAllBlockObservers
Discussion

Remove all registered block observers.

Declared In

NSObject+BKBlockObservation.h

bk_removeObserverForKeyPath:identifier:

- (void)bk_removeObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)token
Discussion

Removes a block observer.

Parameters

keyPath

The property to stop observing, relative to the reciever.

token

The unique key returned by addObserverForKeyPath:task: or the identifier given in addObserverForKeyPath:identifier:task:.

Declared In

NSObject+BKBlockObservation.h

bk_removeObserversWithIdentifier:

- (void)bk_removeObserversWithIdentifier:(NSString *)token
Discussion

Removes multiple block observers with a certain identifier.

Parameters

token

A unique key returned by addObserverForKeyPath:task: and addObserverForKeyPaths:task: or the identifier given in addObserverForKeyPath:identifier:task: and addObserverForKeyPaths:identifier:task:.

Declared In

NSObject+BKBlockObservation.h