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:

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.”


ReactiveCocoa UIKonf 2013 presentation slides

ReactiveCocoa Essentials: Understanding and Using RACCommand

Tutorial: Building A Weather App With Functional Objective-C Programming Using ReactiveCocoa

Replacing the Objective-C “Delegate Pattern” with ReactiveCocoa

Video Introduction To ReactiveCocoa And Tutorial On Reactive Cocoa In A Data-Driven Application

ReactiveCocoa Tutorial – The Definitive Introduction: Part 1/2 and Part 2/2

If KVO is right, why does it feel so wrong?

A First Look at ReactiveCocoa 3.0

An Introduction to ReactiveCocoa

ReactiveCocoa and MVVM, an Introduction

Alex | March 27, 2013
  • Richard Turton March 27, 2013 at 1:11 pm
    Thanks for the link! I've been reading your blog for a while and did something of a double take when I saw my little project linked there. Only goes to show that RAC examples are few and far between at the moment!
    • Alex Curylo March 27, 2013 at 1:26 pm
      No problem. I searched pretty thoroughly for examples, you definitely deserve whatever credit there is for jumping out front with this :)

Leave a Reply