When ReactiveCocoa was released last year I gave it a quick look, but it didn’t really click with me, which made me feel dumb so I made the usual mistake of dismissing it.
(sidenote: in a previous life I learnt the hard way to not dismiss the work of Github, but that’s an entirely different story and circumstances. Old dog. No new tricks).

A few weeks ago I decided to give it a proper look and integrate it into Sequence. This post isn’t meant as a tutorial or even an introduction, it’s just a collection of things that weren’t immediately obvious to me. I suggest reading this and this, as well as the official documentation, which does a better job than me at introducing the high-level concepts.

Use RAC() and RACAble() to easily bridge between worlds

RAC() lets you assign a property to a signal, meaning that whenever the signal sends its next event it’ll set the property to the value of that signal.

RACAble() as well as RACAbleWithStart() creates a signal which acts as an observer for the given key path. RACAbleWithStart simply starts with the current value as well. I’ve been using this a lot instead of the traditional cocoa KVO:

Of course, the real power of ReactiveCocoa is that signals are composable, looking at our textChanged signal again we can throttle the rate at which we receive the signal, for instance to avoid unnecessary network requests (think autocompletion of remote values) or keeping UI updates reasonable

Use RACSubject for bridging between worlds in a controlled manner

RACSubject is a signal which you can manually control. This is extremely useful when bridging between worlds and you want to control when the signal is sent (as opposed to just when a property changes). For instance in Sequence I manually send a signal through a RACSubject whenever the marker is moved in the timeline. My timeline class simply exposes the RACSubject as a RACSignal externally:

I return the internal frameSubject from the frameChanged property, thereby hiding the implementation detail of the fact that I use a RACSubject internally. Furthermore, frameChanged starts by sending 0 and only sends events if the value of the signal actually changes.

A useful subclass of RACSubject is RACReplaySubject, which stores any values sent to it and replays it to new subscribers. This is very useful for network requests or other heavy work you only need to do once.

Using it

It took a while for me to get ReactiveCocoa and conceptually it’s very different to the style of imperative programming I usually do. But even though the abstractions seem high I certainly think it’s been worth it integrating it into Sequence.

I admit I don’t currently harness the full power of ReactiveCocoa, as I don’t do a lot of signal composing. I’ve been meaning to refactor parts of the rendering pipeline in Sequence to use ReactiveCocoa, as it really could benefit of a cleaner abstracting of the inter-dependent steps it needs to run through in order to analyze and apply the deflickering to the photos. But I’m starting slow, replacing the UI related parts that previously relied on bindings and KVO to use signals instead and overall I think it comes out nicer and easier to understand and work with, provided you know how ReactiveCocoa actually works of course.

As always, the best way to learn something is to just jump in and use it in a real project.