1 Comment

ReactiveCocoa: The Future of Cocoa Programming

Last year at around this time, Github announced ReactiveCocoa. I was excited to see a Functional Reactive Programming framework made for Objective-C and found an immediate use for it in an iOS project. Since its release, there’s been a tremendous amount of activity, documentation, and blog posts around it.

The point being, there isn’t an excuse not to use it anymore, and I’d argue that it is (excuse the reference) the Rearden Metal of Objective-C frameworks. It will significantly improve the structure and reduce the complexity of your code. It removes the need for the delegate pattern, continuation-passing style programming, and (most importantly) keeping track of state.

A Quick Example

Below is a small example of how I have used ReactiveCocoa to react to the change in the value of an object.

The code is waiting for the LED light property to change in value, and when it does, it will need to change the LED brightness indicator in the UI. Note that it is using the distinctUntilChanged modifier which ensures it won’t react unless the value has actually changed as opposed to just being set.

@weakify(self);
[[RACAble(self.device.LEDBrightness) 
  distinctUntilChanged]
 subscribeNext:^(NSNumber *LEDBrightness) {
     @strongify(self);
     [self updateUI];
 }];            

Without ReactiveCocoa, the code would have to be restructured to use a delegate pattern or KVO (which is very clumsy) to watch the value of the LEDBrightness property. Worse, I would’ve had to create a variable to capture the prior state of the value to ensure that it wasn’t unnecessarily updating the UI.

Resources

If you want to learn more about ReactiveCocoa here are list of resources I found helpful: