Slide 1

Slide 1 text

BE PROACTIVE USE REACTIVE

Slide 2

Slide 2 text

iOS @ NY Times @cdzombak github.com/cdzombak chris dzombak [email protected]

Slide 3

Slide 3 text

What is reactive programming?

Slide 4

Slide 4 text

“ ” reactive |rēˈaktiv| … a programming paradigm oriented around data flows and the propagation of change

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

reactive programming data flows propagation of change =

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

we all respond to flowing data and react to changes Model View Controller

Slide 12

Slide 12 text

Cocoa’s KVO? NSNotificationCenter? Ember or Angular observers? Callbacks? Listeners? How is this different from… Programming?

Slide 13

Slide 13 text

Back to the basics

Slide 14

Slide 14 text

Subject Observer notify()

Slide 15

Slide 15 text

DATA STREAMS

Slide 16

Slide 16 text

Data type a value over time

Slide 17

Slide 17 text

•updated property values •mouse cursor position •typing characters into a text field •database records

Slide 18

Slide 18 text

•updated property values •mouse cursor position •typing characters into a text field •database records

Slide 19

Slide 19 text

Collection of emitted events ordered in time

Slide 20

Slide 20 text

with functions for observation

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

onNext( )

Slide 23

Slide 23 text

onNext( )

Slide 24

Slide 24 text

onNext( )

Slide 25

Slide 25 text

onComplete()

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

onNext( )

Slide 28

Slide 28 text

onNext( )

Slide 29

Slide 29 text

onError() x

Slide 30

Slide 30 text

[ "value”, "other value”, "another value” ]

Slide 31

Slide 31 text

[ "value”, "other value”, "another value” ] "a value",

Slide 32

Slide 32 text

[ "value”, "other value”, "another value” ] "a value", "other value",

Slide 33

Slide 33 text

[ "value”, "other value”, "another value” ] "a value", "other value", "another value"

Slide 34

Slide 34 text

textField.rac_textSignal() .subscribeNext({ (value) -> Void in // value = “H”, “He”, … })

Slide 35

Slide 35 text

textField.rac_textSignal() .subscribeNext({ (value) -> Void in // value = “H”, “He”, … })

Slide 36

Slide 36 text

apiClient.performRequest(request) .subscribeNext({ (response) -> Void in // do something with response }, error: { (error) -> Void in println(“Error: \(error)”) }, completed: { () -> Void in println(“Done.”) })

Slide 37

Slide 37 text

Stream.transform(f: (x) -> y) -> Stream Streams are composable

Slide 38

Slide 38 text

1 3 2 2 6 4 map { (x) -> Int in x * 2 } Streams are composable

Slide 39

Slide 39 text

1 2 4 filter { (x) -> Bool in x % 2 == 0 } 4 2 Streams are composable

Slide 40

Slide 40 text

Double tap! Demo

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }

Slide 44

Slide 44 text

taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }

Slide 45

Slide 45 text

taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }

Slide 46

Slide 46 text

1 2 3 taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }

Slide 47

Slide 47 text

2 taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") }

Slide 48

Slide 48 text

taps .bufferWithTime(0.5) .map { ($0 as RACTuple).count } .filter { ($0 as Int) == 2 } .subscribeNext { (_) in println("Double tap!") } Double tap!

Slide 49

Slide 49 text

RACSignal.combineLatest([ slowToEmitStream, evenSlowerToEmitStream, ]) .doNext { let tuple = $0 as RACTuple processResults( tuple.first, tuple.second) } .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeCompleted { () -> Void in println("The work is done!") }

Slide 50

Slide 50 text

MODEL WITH STREAMS

Slide 51

Slide 51 text

MODEL WITH STREAMS BUT…WHY?

Slide 52

Slide 52 text

Claim code should communicate intent

Slide 53

Slide 53 text

Current tooling is insufficient

Slide 54

Slide 54 text

map extension Array { func map(transform: (T) -> U) -> [U] }

Slide 55

Slide 55 text

let input = [1, 2, 3] var output = [Int]() for number in input { output.append(number * 2) } // output: [2, 4, 6] let output = [1, 2, 3].map { $0 * 2 } // output: [2, 4, 6]

Slide 56

Slide 56 text

let input = [1, 2, 3] var output = [Int]() for number in input { output.append(number * 2) } // output: [2, 4, 6] let output = [1, 2, 3].map { $0 * 2 } // output: [2, 4, 6] Concept: 1-1 mapping

Slide 57

Slide 57 text

map reifies 1-1 data transformation

Slide 58

Slide 58 text

Good Thing™ reify |ˈrēəәˌfī| make (something abstract) more concrete or real

Slide 59

Slide 59 text

reactive programming reifies event-driven software

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

var count = 0 upButton.addTarget(self, action: “upButtonTouched:”, forControlEvents: .TouchUpInside) downButton.addTarget(self, action: “downButtonTouched:", forControlEvents: .TouchUpInside) countLabel.text = String(count) func upButtonTouched(sender: UIButton) { count++ countLabel.text = String(count) } func downButtonTouched(sender: UIButton) { count-- countLabel.text = String(count) }

Slide 63

Slide 63 text

let upTaps = upButton .rac_signalForControlEvents(.TouchUpInside) let downTaps = downButton .rac_signalForControlEvents(.TouchUpInside) let count = RACSignal.merge([ RACSignal.return(0), upTaps.mapReplace(1), downTaps.mapReplace(-1) ]) .scanWithStart(0, reduce: { $0 + $1 }) .map { String($0) } count ~> RAC(self, "countLabel.text")

Slide 64

Slide 64 text

client.loginWithSuccess({ client.loadCachedTweetsWithSuccess({ (tweets) in client.fetchTweetsAfterTweet(tweets.last, success: { (tweets) -> Void in // Now we can show our tweets }, failure: { (error) -> Void in presentError(error) }) }, failure: { (error) -> Void in presentError(error) }) }, failure: { (error) -> Void in presentError(error) })

Slide 65

Slide 65 text

client.login() .then { return client.loadCachedTweets() } .flattenMap { (tweets) -> RACStream in return client.fetchTweetsAfterTweet(tweets.last) } .subscribeError({ (error) -> Void in presentError(error) }, completed: { () -> Void in // Now we can show our tweets })

Slide 66

Slide 66 text

bring changes over time into your type system

Slide 67

Slide 67 text

/// Clients should observe via property observer: public var title: String public let title: Signal

Slide 68

Slide 68 text

func loginWithSuccess( success: () -> Void, failure: (NSError) -> Void) { // function doesn’t return anything // it jumps into two callbacks based on return } func login() -> RACSignal { // function has a native return value // a data stream object that we can observe }

Slide 69

Slide 69 text

becomes easier concurrency

Slide 70

Slide 70 text

Scheduler 
 schedules when and where work is performed
 
 reactive concurrency

Slide 71

Slide 71 text

Scheduler 
 schedules when and where work is performed
 
 deliverOn(Scheduler) subscribeOn(Scheduler) reactive concurrency

Slide 72

Slide 72 text

SO… HOW DO I DO THIS? (photo by Andrew Sardone)

Slide 73

Slide 73 text

ReactiveCocoa ReactiveExtensions RxJava Bacon.js Elm github.com/ReactiveCocoa/ReactiveCocoa github.com/Reactive-Extensions github.com/ReactiveX/RxJava github.com/baconjs/bacon.js elm-lang.org

Slide 74

Slide 74 text

SOURCES FURTHER READING

Slide 75

Slide 75 text

Missing RX Intro Input & Output RxMarbles Other j.mp/missingrxintro j.mp/joshaberio rxmarbles.com pinboard.in/u:cdzombak/t:reactiveprogramming

Slide 76

Slide 76 text

B Y E (photo by Chris Dzombak) (puppy by Shane Bliemaster)