Over a million developers have joined DZone.


· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Noticed mentions of this “ReactiveCocoa” thing popping up all around lately? Yeah, us too. Let’s take a bit of a look at what that is exactly, shall we?

ReactiveCocoa / ReactiveCocoa: “is an Objective-C framework for Functional Reactive Programming. It provides APIs for composing and transforming streams of values.”

Not feeling the excitement yet? Understandable. So what is this FRP thing exactly?

Functional Reactive Programming (FRP) is a programming paradigm for writing software that reacts to change.

FRP is built on the abstraction of values over time. Rather than capturing a value at a particular time, FRP provides signals that capture the past, present, and future value. These signals can be reasoned about, chained, composed, and reacted to.

By combining signals, software can be written declaratively, without the need for code that continually observes and updates values. A text field can be directly set to always show the current timestamp, for example, instead of using additional code that watches the clock and updates the text field every second.

Signals can also represent asynchronous operations, much like futures and promises. This greatly simplifies asynchronous software, including networking code…

If that’s a bit too abstract to get a handle on easily, NSHipster has a nice layman’s intro:

ReactiveCocoa is comprised of two major components: signals (RACSignal) and sequences (RACSequence).

Both signals and sequences are kinds of streams, sharing many of the same operators. ReactiveCocoa has done well to abstract a wide scope of functionality into a semantically dense, consistent design: signals are a push-driven stream, and sequences are a pull-driven stream…

So, it’s basically fancy-pants KVO, then? No, that’s underestimating it, check out this ReactiveCocoa: First Impressions post:

‘So what?’, I hear you say, ‘It’s a nice wrapper for KVO, big whoop’. Well, it gets better: in addition to a simply KVO API, ReactiveCocoa lets you express complex conditional behaviours.

GitHub’s example is as follows:

[[[[RACAbleSelf(self.username) distinctUntilChanged] take:3]
     where:^(NSString *newUsername) {
        return [newUsername isEqualToString:@"joshaber"];
     subscribeNext:^(id _) {
        NSLog(@"Hi me!");

Here’s their explanation of what’s going on:

We watch username for changes, filter out non-distinct changes, take only the first three non-distinct values, and then if the new value is “joshaber”, we print out a nice welcome.

Using KVO alone this simple isn’t possible: we would need to track and retain the value of username to compare it with the new value and keep a counter around to increment every time we receive a non-distinct value. This would certainly be more than six lines of code…

OK, so it’s like KVO, plus Cocoa Bindings, except not sucking. Well, that is pretty nifty. Definitely something to try out next time we have a complex pattern to implement!

Here’s some more discussion posts worth reading:

Input and Output

Better Code for a Better World : “… an introduction to using ReactiveCocoa to solve all of the problems currently facing mankind—a phrase I only use somewhat facetiously…”

Basic MVVM with ReactiveCocoa

And some coding samples:

ReactiveCocoa / ReactiveCocoaIO: “a framework for accessing and manipulating a file system through signals…”

ReactiveCocoa / ReactiveCocoaLayout: “a framework for describing Cocoa and Cocoa Touch layouts in a reactive way…”

uasi / AFNetworking-ReactiveCocoa “makes AFNetworking reactive.”

UIButton Edge Insets has both RAC and non-RAC implementations of button editing code.

Machx / Reactive-Cocoa-Playground: “a collection of useful and sometimes experimental ReactiveCocoa code.”

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


Published at DZone with permission of Alex Curylo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}