Slide 1

Slide 1 text

ReactiveCocoa Lessons Learned Rob Pearson @robpearson

Slide 2

Slide 2 text

Maple Pixel Everyday Transit 1.0 coming soon ...

Slide 3

Slide 3 text

FrieNDA

Slide 4

Slide 4 text

Everyday ReactiveCocoa 1. Functional Programming Briefly 2. Signals and Pipelines 3. RAC Lessons Learned

Slide 5

Slide 5 text

Functional Programming Briefly

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Signals and Pipelines

Slide 9

Slide 9 text

Demo

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Transit Dashboard Pipeline Inputs: * List of Everyday Trips (Favourites) * GPS Location * Time Outputs: * Next Transit Trip

Slide 12

Slide 12 text

Pipelines == RACSignals

Slide 13

Slide 13 text

RACSignal Example [RACSignal createSignal:^(id

Slide 14

Slide 14 text

So we have Pipelines/ Signals. Now what?

Slide 15

Slide 15 text

Two Options RAC(self, viewModel.something) RACSignal subscribeNext: error: completed:

Slide 16

Slide 16 text

RAC Lessons Learned

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

KVO

Slide 19

Slide 19 text

No, Seriously ... KVO!

Slide 20

Slide 20 text

Key Value Observing // Bind Transit Trips to Table View [RACObserve(self.viewModel, everydayTransitTrips) subscribeNext:^(id x) { @strongify(self); [self.tableView reloadData]; }];

Slide 21

Slide 21 text

Work with Protocols

Slide 22

Slide 22 text

rac_signalForSelector [[self rac_signalForSelector:@selector(searchBar:textDidChange:) fromProtocol:@protocol(UISearchBarDelegate)] subscribeNext:^(RACTuple *value) { @strongify(self); UISearchBar *searchBar = value.first; if (searchBar == self.departingLocationsSearchBar) { [self.viewModel filterDepartingLocationsByName:self.departingLocationsSearchBar.text]; } else { [self.viewModel filterArrivingLocationsByName:self.arrivingLocationsSearchBar.text]; } }];

Slide 23

Slide 23 text

Take advantage of RAC Category Methods

Slide 24

Slide 24 text

NotificationCentre RACSignal *appActiveSignal = [[[[NSNotificationCenter.defaultCenter rac_addObserverForName:UIApplicationDidBecomeActiveNotification object:nil] mapReplace:@YES] startWith:@YES] setNameWithFormat:@"%@ appActive", self];

Slide 25

Slide 25 text

Signal Tips and Tricks

Slide 26

Slide 26 text

Reactive Timer 1 Map Time

Slide 27

Slide 27 text

Reactive Timer 1 [[[[[RACSignal interval:60 onScheduler:[RACScheduler scheduler]] map:^id(NSDate *timestamp) { @strongify(self); if (self.hasEverydayTrips != nil && [@(YES) isEqualToNumber:self.hasEverydayTrips]) { return @"SEQ"; } else { return @"Everyday Transit"; } }] startWith:@"Everyday Transit"] distinctUntilChanged] deliverOn:[RACScheduler mainThreadScheduler]];

Slide 28

Slide 28 text

Reactive Timer 2 Empty Signal with a delay.

Slide 29

Slide 29 text

Reactive Timer 2 RACSignal *nextTransitTripIntervalSignal = [RACSignal interval:1 onScheduler:[RACScheduler scheduler]]; RACSignal *currentUserLocationRefreshDelay = [[RACSignal empty] delay:60]; RACSignal *currentUserLocationRefreshSignal = [[[self.locationService.locationSignal take:1] concat:currentUserLocationRefreshDelay] repeat];

Slide 30

Slide 30 text

Real Power is combing and chaining signals

Slide 31

Slide 31 text

Protips • Start by reading IntroToRx.com • Start small and iterate. • Logging w/ Something like Cocoalumberjack • Asks questions by opening issues at http://github.com/ ReactiveCocoa/

Slide 32

Slide 32 text

Challenges • Thinking like a Functional Programmer • ReactiveCocoa Doco • Debugging

Slide 33

Slide 33 text

References • Github Repo: http://github.com/ReactiveCocoa/ • Ray Wenderlich Tutorial: https://bit.ly/1rXA31Y • Big Nerd Ranch Tutorial: https://bit.ly/1mp04mI • FRP on iOS by Ash Furrow: https://leanpub.com/iosfrp • Brent Simmons on ReactiveCocoa: https://bit.ly/PcyjCL

Slide 34

Slide 34 text

Questions?