ReactiveCocoa & Complex User Interfaces

“ReactiveCocoa”: is an “FRP”: framework written for Objective-C that makes it easy to observe, compose, and transform values. Recently, I had an opportunity to truly leverage the power of ReactiveCocoa when I found myself facing a complex user interface that took a multitude of inputs from the user.

h2. The Problem

The UI we were developing had the following features:
* A table of rows containing values that represent primary and optional secondary values.
* Each row’s primary and secondary values represent user inputs from a corresponding form.
* When the user submits the _composite_ form the associated form’s data must be merged.

Below is a rough sketch of the UI:


h2. The Toolbox

I tackled the above problem leveraging the following frameworks:

* “RubyMotion”: – A toolchain that allows developers to create iOS applications using the Ruby language
* “ReactiveCocoa”:
* “Formotion”: – A great library for creating iOS forms easily.
* “RubyMotion extensions”: to ReactiveCocoa

h3. ReactiveCocoa’s Objective-C to Ruby Legend

For those of you that are familiar with Objective-C but not Ruby, you may find the legend below helpful.

* is analogous to RACObserve(self, property)
* = signal is analogous to RAC(self, property) = signal

h2. The Form

The center piece of the above mockup is the form (plus the user inputs and data returned by the form once it has been submitted). The first thing to do is track the form data as it changes, which is when the user submits the form.

class RACForm
  attr_reader :form, 

  def initialize(form, primary_key, *secondary_keys)
    @form = form
    @data_signal = RACReplaySubject.replaySubjectWithCapacity RACReplaySubjectUnlimitedCapacity

    self.data_signal.sendNext @form.render

    @form.on_submit do |f|
      self.data_signal.sendNext @form.render