31
Reactive Cocoa Robert Brown Twitter: @robby_brown ADN: @robert_brown

Reactive Cocoa

Embed Size (px)

DESCRIPTION

The basics of Reactive Cocoa. The tips and tricks in this presentation will cover almost all the use cases for Reactive Cocoa. Demo here: https://github.com/rob-brown/Demos/tree/master/RACDemo.

Citation preview

Page 1: Reactive Cocoa

Reactive CocoaRobert Brown Twitter: @robby_brown ADN: @robert_brown

Page 2: Reactive Cocoa

What is Reactive Cocoa?

A framework for Functional Reactive Programing (FRP)

Developed by GitHub

Page 3: Reactive Cocoa

What is Functional Reactive Programming?

Page 4: Reactive Cocoa

–Wikipedia

“Functional reactive programming (FRP) is a programming paradigm for reactive programming

using the building blocks of functional programming.”

Page 5: Reactive Cocoa

What are the building blocks of functional programming?

!

reduce/fold

map

filter

zip

!

drop

take

concat

and more

Page 6: Reactive Cocoa

What is functional programming?

Immutable data

Limited state

Pure functions

Page 7: Reactive Cocoa

Immutable Data

Data structures can’t be modified in place

Values are copied as needed

NSArray vs. NSMutableArray

Page 8: Reactive Cocoa

Limited State

No global data

No side effects

Page 9: Reactive Cocoa

Pure Functions

Given input X, the function always returns Y

Allows for many compiler-level optimizations

Page 10: Reactive Cocoa

Higher Order Functions

Page 11: Reactive Cocoa

Map

1

2

3

2

4

6

x * 2

5

6

7

x + 4

Page 12: Reactive Cocoa

Reduce / Fold

1

2

3

0

Sum

136

Page 13: Reactive Cocoa

How do I use Reactive Cocoa?

Page 14: Reactive Cocoa

RACSignal

RACSignal is a stream of data, possibly infinite

Its values are evaluated lazily

Signals are the most used objects in Reactive Cocoa

Page 15: Reactive Cocoa

RAC as KVO

Advantages:

No more hard-coded key paths

No more comparing key paths

Code locality

Refactoring will update RAC bindings

Page 16: Reactive Cocoa

RAC as KVO

Disadvantages:

Watch out for retain cycles

All objects are of type id

Page 17: Reactive Cocoa

RAC as KVO

[RACObserve(self, name) subscribeNext:

^(NSString * name) {

NSLog(@“Name changed: %@”, name);

}];

Page 18: Reactive Cocoa

RAC Bindings

Like Cocoa Bindings

Automatically changes properties

Page 19: Reactive Cocoa

RAC Bindings

RAC(self, textField2.text) = self.textField1.rac_textSignal;

!

RAC(self, nameField.text) = RACObserve(self, name);

Page 20: Reactive Cocoa

Tips and Tricks

Transform the values of a signal:

[signal map:^id(NSString * text) {

return [text uppercaseString];

}];

Page 21: Reactive Cocoa

Tips and TricksCombine the results of many signals:

[RACSignal combineLatest:@[s1, s2] reduce:

^id(NSNumber * b1, NSNumber * b2) {

return @([b1 boolValue] &&

[b2 boolValue]);

}];

Page 22: Reactive Cocoa

Tips and Tricks

Watch for when multiple tasks complete:

[[RACSignal merge:@[signal1, signal2] subscribeCompleted:^{

NSLog(@“All done”);

}];

Page 23: Reactive Cocoa

Tips and TricksAdd actions to buttons:

self.button.rac_command =

[[RACCommand alloc] initWithSignalBlock:^RACSignal *(id x) {

// Code goes here

};

Page 24: Reactive Cocoa

Tips and TricksCreate arbitrary signals:

[RACSignal createSignal:

^RACDisposable *(id<RACSubscriber> subscriber) {

// Send next, error, and completed

// messages to subscriber

}];

Page 25: Reactive Cocoa

Tips and Tricks

Deliver signal results to the main thread:

[backgroundSignal deliverOn:

[RACScheduler mainThreadScheduler]];

Page 26: Reactive Cocoa

Tips and Tricks

If you need to perform side effects:

[signal doNext:^(id value) {…}];

[signal doError:^(NSError * error) {…}];

[signal doCompleted:^{…}];

Page 27: Reactive Cocoa

Tips and Tricks

Limit the rate a signal can be evaluated:

[signal throttle:10.0];

Page 28: Reactive Cocoa

Questions?

Page 29: Reactive Cocoa

Demo

Page 31: Reactive Cocoa

Other Implementations

C♯ Reactive Extensions (Rx)

JavaScript react.js