Reactive Cocoa is an Objective-C framework that treats streams of values as signals that can be combined, transformed, and manipulated. It is based on the concept of functional reactive programming and provides signals to represent user interface elements and other values that change over time. This allows UI updates and other behaviors to be modeled as signals that can be combined declaratively.
2. Reactive Cocoa
Reactive Cocoa is a third-party Objective C
framework which has received some interested and a
community-following.
3. Functional Programming
Functional programming is a style of programming based on a formal model of computation called lambda calculus.
Some programming languages such as Lisp (and its children Scheme and Clojure), Haskell, Scala, ML called functional
programming languages because they are very amenable to this style of programming, although it can technically be
done with many other languages.
Languages like C, Objective C, and Java are called imperative because programs are often written in a different style and
these languages support that style.
For our purposes, functional programming means programs are composed of functions.
f(x) = x+5, g(x) = x+2
We can evaluate functions which are composed together by substitution.
f(g(4))
f(4+2)
f(6)
6+5
11
Given an input, we always get the same output.
4. Functional programming tend to operate on lists, which are seen as a sequences of values.
list = (1, 2, 3)
Calling it a sequence is important. Generally, lists are treated as the first element (head) and
the rest of the elements besides the head.
list.head = 1, list.tail = (2, 3)
This is different from an array where any element can be accessed at one time.
list[2]
This approach has the advantage of allowing some programs to be written in a recursive
style.
int maxValue(currentMax, list) {
if (list.empty) return currentMax
else if (list.head > currentMax) return maxValue(list.head, list.tail)
else maxValue(currentMax, list.tail)
}
Also: infinite streams.
5. The ability to have that kind of mutable state is forbidden in
pure functional programming. In practice, pure functional
programming does not have assignment.
(Maybe languages that support functional programming do,
in fact, allow assignment.)
It turns out, this is an excellent feature for designing
applications that work with concurrency. This is one
reason why functional programming has received a lot of
interest recently (and why, for example, Apple brought
blocks to C / Objective C in 2009).
6. But following this rule presents a challenge for
working with user input.
Imagine you wanted to write a function which gets
keystrokes one character at a time.
char getCharacter();
This function is not really compatible with functional
styles of programming, because it might return any
character from the same input (in this case,
nothing).
8. Functional Reactive Programming
There are a lot of various implementations of
Functional Reactive Programming (e.g. Elm).
It handles the problems of dealing with user input by
treating it as the parameters of functions, executing
them again and again as the input changes.
function ( ) {
}
9. Reactive Cocoa
Based on Microsoft’s Rx Extensions.
Developed by engineers at GitHub.
It is quite large. It may be best to consider it an
extension to the Objective C language.
My plan is only to demonstrate some practical
examples, focusing on the concept of signals.
14. Note on Concurrency
No two subscription blocks can be called
simultaneously to avoid deadlock.
You can control which thread the subscription block is
called on with RACScheduler
This allows for signals to mimic an
NSOperationQueue.
15. Conclusion
ReactiveCocoa is based on some relatively abstract
concepts but these concepts turn out to be very, very
powerful in practice.
Signals, for example, may be able to replace KVO,
operation queues and delegation in an app. I have
barely touched the available feature set in these
examples.
16. Conclusion
It does require a lot of abstract thinking to determine
how to model your apps behavior as a series of
signals.
For this reason, there is a learning curve associated
with it.
Personally, I am very likely use it in the future.