No hassle Objective-C blocks-based KVO library with automatic removal
There are so many KVO libraries out there. Couldn't find one that supports both blocks and automatic removal of observers. And that's usually what I need. Just want to bind stuff around.
CLBlockObservation is available through CocoaPods, to install it simply add the following line to your Podfile:
pod "CLBlockObservation"
Usage is straightforward. I would recommend including <NSObject+CLBlockObservation.h>
in your .pch
file.
[observee addObserver:observer keyPath:@"p1" block:^(id oldValue, id newValue) {
NSLog(@"do something great!");
}];
There's also support for observing multiple keypaths:
[observee addObserver:observer keyPaths:@[ @"p1", @"p2" ] block:^{
NSLog(@"be awesome!");
}];
There's an XCTest project in this repository. It contains a few tests that verify the lifetime management of the observation and a good sample reference.
There's a script that runs the tests from command line:
$ clone https://github.com/eladb/CLBlockObservation
$ cd CLBlockObservation/Tests
$ ./run-tests.sh
Elad Ben-Israel, [email protected]
CLBlockObservation is available under the MIT license. See the LICENSE file for more info.