Observing Many, Notifying One

(This is a repost from my old blog at blogger.com.)

I’ve made an Objective C framework for observing (a combination of) many properties, while triggering only one notification callback, whenever the multi-observation criteria is fulfilled.

Sounds fancy, but the need is quiet simple. The canonical example is a login form with a name and a password, plus a Login button which is only enabled when the name and password are not empty.

An alternative example is a checklist with an action, that should only be triggered when all items on the list are checked.

The framework has an elegant way to express these cases. Here is how the first use case would be expressed, by multi-observing the AND combination of the relevant properties:

PIMultiObserver *multiObserver = [PIMultiObserver new];
[multiObserver observeAnd:@[
    [PIObserver observerOf:self keyPath:@"name.length"],
    [PIObserver observerOf:self keyPath@"password.length"]]
    block:^(BOOL combinedValue) {
        self.loginButton.hidden = !combinedValue;
    }];

And here is the checklist use case, using the observeAllYes method, which only triggers the notification block when all observed properties evaluate true:

[multiObserver observeAllYes:@[
    [PIObserver observerOf:self keyPath:@"booster"],
    [PIObserver observerOf:self keyPath@"retro"],
    [PIObserver observerOf:self keyPath@"fido"],
    [PIObserver observerOf:self keyPath@"guidance"],
    [PIObserver observerOf:self keyPath@"surgeon"]]
    block:^(BOOL combinedValue) {
        NSLog(@"All systems go!");
    }];

Individual observers participating in the multi-observation can also map their observed properties to a BOOL by defining a mapper function.

The code is freely available on Github: https://github.com/pipacs/multiobserver.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s