Wednesday, October 21, 2015

Observing Many, Notifying One

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.

No comments:

Post a Comment