Slide 1

Slide 1 text

ReactiveCocoa and Swift Better Together @ColinEberhardt ShinobiControls

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

[[[[[[[[[[[[

Slide 5

Slide 5 text

f*****gblocksyntax.com

Slide 6

Slide 6 text

ReactiveCocoa In ten minutes

Slide 7

Slide 7 text

Functional Reactive Programming Functional Programming Reactive Programming + =

Slide 8

Slide 8 text

ReactiveCocoa In my own words

Slide 9

Slide 9 text

Every line of code we write is executed in reaction to an event

Slide 10

Slide 10 text

But … these events come in many different forms

Slide 11

Slide 11 text

ReactiveCocoa provides a common interface for all events

Slide 12

Slide 12 text

… this allows us to define a language for manipulating, transforming and coordinating events

Slide 13

Slide 13 text

ReactiveCocoa in action

Slide 14

Slide 14 text

RACSignal *textSignal = [self.usernameTextField rac_textSignal]; [textSignal subscribeNext:^(id x) { NSLog(x); }];

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Signals emit events to their subscribers

Slide 17

Slide 17 text

… they emit none, one or more next events, optionally followed by either an error or completed NEXT NEXT NEXT NEXT … NEXT NEXT ERROR COMPLETED

Slide 18

Slide 18 text

Signal All Things!

Slide 19

Slide 19 text

RACSignal *textSignal = [self.usernameTextField rac_textSignal]; RACSignal *filteredText = [textSignal filter:^BOOL(NSString *text) { return text.length > 3; }]; [filteredText subscribeNext:^(id x) { NSLog(x); }];

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

What are events? and what do they look like?

Slide 22

Slide 22 text

anything!

Slide 23

Slide 23 text

RACSignal *textSignal = [self.usernameTextField rac_textSignal]; RACSignal *textLength = [textSignal map:^id(NSString *text) { return @(text.length); }]; [textLength subscribeNext:^(id x) { NSLog(@"%@", x); }];

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

RACSignal *textSignal = [self.usernameTextField rac_textSignal]; RACSignal *textLength = [textSignal map:^id(NSString *text) { return @(text.length); }]; RACSignal *filteredText = [textLength filter:^BOOL(NSNumber *length) { return [length intValue] > 3; }]; [filteredText subscribeNext:^(id x) { NSLog(@"%@", x); }];

Slide 26

Slide 26 text

[[[[self.usernameTextField rac_textSignal] map:^id(NSString *text) { return @(text.length); }] filter:^BOOL(NSNumber *length) { return [length intValue] > 3; }] subscribeNext:^(id x) { NSLog(@"%@", x); }];

Slide 27

Slide 27 text

rac_textSIgnal- filter- subscribeNext- Value->-3- map- NSString- NSNumber-

Slide 28

Slide 28 text

rac_textSignal- filter- setKeyPath- thro5le- filter- fla5enMap- twi5erSearch- deliverOn- subscribeNext- requestAccess- signal- then-

Slide 29

Slide 29 text

[[[[[[[[self requestAccessToTwitterSignal] then:^RACSignal *{ @strongify(self) return self.searchText.rac_textSignal; }] filter:^BOOL(NSString *text) { @strongify(self) return [self isValidSearchText:text]; }] throttle:0.5] flattenMap:^RACStream *(NSString *text) { @strongify(self) return [self signalForSearchWithText:text]; }] map:^id(NSDictionary *jsonSearchResult) { NSArray *statuses = jsonSearchResult[@"statuses"]; NSArray *tweets = [statuses linq_select:^id(id tweet) { return [RWTweet tweetWithStatus:tweet]; }]; return tweets; }] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSArray *tweets) { [self.resultsViewController displayTweets:tweets]; } error:^(NSError *error) { NSLog(@"An error occurred: %@", error); }];

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

ReactiveCocoa Swift

Slide 34

Slide 34 text

[[[[[[[[self requestAccessToTwitterSignal] then:^RACSignal *{ @strongify(self) return self.searchText.rac_textSignal; }] filter:^BOOL(NSString *text) { @strongify(self) return [self isValidSearchText:text]; }] throttle:0.5] flattenMap:^RACStream *(NSString *text) { @strongify(self) return [self signalForSearchWithText:text]; }] map:^id(NSDictionary *jsonSearchResult) { NSArray *statuses = jsonSearchResult[@"statuses"]; NSArray *tweets = [statuses linq_select:^id(id tweet) { return [RWTweet tweetWithStatus:tweet]; }]; return tweets; }] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSArray *tweets) { [self.resultsViewController displayTweets:tweets]; } error:^(NSError *error) { NSLog(@"An error occurred: %@", error); }];

Slide 35

Slide 35 text

[[[[[[[[[[[[

Slide 36

Slide 36 text

requestAccessToTwitterSignal() .then { self.searchTextField.rac_textSignal() } .filterAs { (text: NSString) -> Bool in text.length > 3 } .doNext { (any) in self.tweetsTableView.alpha = 0.5 } .throttle(0.5) .flattenMapAs { (text: NSString) -> RACStream in self.signalForSearchWithText(text) } .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs ({ (tweets: NSDictionary) in let statuses = tweets["statuses"] as [NSDictionary] self.tweets = statuses.map { Tweet(json: $0) } self.tweetsTableView.reloadData() self.tweetsTableView.scrollToTop() self.tweetsTableView.alpha = 1.0 }, { (error) in println(error) })

Slide 37

Slide 37 text

requestAccessToTwitterSignal() .then { self.searchTextField.rac_textSignal() } .filterAs { (text: NSString) -> Bool in text.length > 3 } .doNext { (any) in self.tweetsTableView.alpha = 0.5 } .throttle(0.5) .flattenMapAs { (text: NSString) -> RACStream in self.signalForSearchWithText(text) } .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs ({ (tweets: NSDictionary) in let statuses = tweets["statuses"] as [NSDictionary] self.tweets = statuses.map { Tweet(json: $0) } self.tweetsTableView.reloadData() self.tweetsTableView.scrollToTop() self.tweetsTableView.alpha = 1.0 }, { (error) in println(error) })

Slide 38

Slide 38 text

Swift ReactiveCocoa

Slide 39

Slide 39 text

Swift encourages immutability

Slide 40

Slide 40 text

51 variables

Slide 41

Slide 41 text

39 constants 12 variables 6 outlets (not my fault!) 1 UIWindow 5 UI state variables

Slide 42

Slide 42 text

ReactiveCocoa 3

Slide 43

Slide 43 text

public final class Signal { ... }

Slide 44

Slide 44 text

requestAccessToTwitterSignal() .then { self.searchTextField.rac_textSignal() } .filterAs { (text: NSString) -> Bool in text.length > 3 } .doNext { (any) in self.tweetsTableView.alpha = 0.5 } .throttle(0.5) .flattenMapAs { (text: NSString) -> RACStream in self.signalForSearchWithText(text) } .deliverOn(RACScheduler.mainThreadScheduler()) .subscribeNextAs ({ (tweets: NSDictionary) in let statuses = tweets["statuses"] as [NSDictionary] self.tweets = statuses.map { Tweet(json: $0) } self.tweetsTableView.reloadData() self.tweetsTableView.scrollToTop() self.tweetsTableView.alpha = 1.0 }, { (error) in println(error) })

Slide 45

Slide 45 text

requestAccessToTwitterSignal() |> then (textField.rac3_textSignal()) |> filter { countElements($0) > 3 } |> throttle(0.5, onScheduler: QueueScheduler.mainQueueScheduler) |> doNext { text in self.tweetsTableView.alpha = 0.5 } |> flattenMap { self.signalForSearchWithText($0) } |> observeOn(QueueScheduler.mainQueueScheduler) |> observe(next: { tweetsDictionary in let statuses = tweetsDictionary["statuses"] as [NSDictionary] self.tweets = statuses.map { Tweet(json: $0) } self.tweetsTableView.reloadData() self.tweetsTableView.alpha = 1.0 })

Slide 46

Slide 46 text

pipe forward operator |>

Slide 47

Slide 47 text

public final class Signal { func map (…) -> Signal }

Slide 48

Slide 48 text

public final class Signal { } func map (…) -> Signal

Slide 49

Slide 49 text

let mapped = map(signal, { $0.foo })

Slide 50

Slide 50 text

let mapped = map(map(signal, { $0.foo }), { $0.bar })

Slide 51

Slide 51 text

let mapped = map(map(map(signal, { $0.foo }), { $0.bar }), { $0.sadface })

Slide 52

Slide 52 text

func map(transform: T -> U) (signal: Signal) -> Signal

Slide 53

Slide 53 text

public func |> (signal: Signal, transform: Signal -> X) -> X { return transform(signal) }

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

func |> (stringy: Stringy, transform: Stringy -> X) -> X { return transform(stringy) }

Slide 56

Slide 56 text

func } |>

Slide 57

Slide 57 text

ReactiveCocoa Swift ReactiveCocoa http://tiny.cc/reactive-swift