reactive cocoa lightning talk

22
ReactiveCocoa Bob Spryn LoHi Labs @sprynmr (github, twitter) https://github.com/ReactiveCocoa/ReactiveCocoa

Upload: m-robert-spryn

Post on 30-Jun-2015

1.296 views

Category:

Technology


0 download

DESCRIPTION

Talking at iOS Camp Denver (http://iosdevcampcolorado.com) about Reactive Cocoa (https://github.com/ReactiveCocoa/ReactiveCocoa) Video here @3:45: http://www.ustream.tv/channel/iosdevcamp-colorado?utm_campaign=t.co&utm_source=ustre-am&utm_medium=social

TRANSCRIPT

Page 1: Reactive Cocoa Lightning Talk

ReactiveCocoaBob Spryn LoHi Labs @sprynmr (github, twitter) !!!https://github.com/ReactiveCocoa/ReactiveCocoa

Page 2: Reactive Cocoa Lightning Talk

ReactiveCocoa• An implementation of functional reactive programming

• It provides APIs for composing and transforming streams of values

• “Signals” send you these values over time for you to react to

http://en.wikipedia.org/wiki/Functional_reactive_programming

Page 3: Reactive Cocoa Lightning Talk

?

Page 4: Reactive Cocoa Lightning Talk

Program Smarter

Not just another API.

It enables you to

Focus on WHAT not HOW

Page 5: Reactive Cocoa Lightning Talk

Reactive!!! Declarative!!! What does that even mean?

Page 6: Reactive Cocoa Lightning Talk

Reactive!!! Declarative!!! What does that even mean?

• Setup your reactions

• Compose, split, filter, etc.

Page 7: Reactive Cocoa Lightning Talk

ImperativeThe way we normally program.

Page 8: Reactive Cocoa Lightning Talk

ImperativeThe way we normally program.

• Observe, wait, then command & control

• Dealing with the HOW, not just the WHAT

• Code for controlling flow is spread all over

Page 9: Reactive Cocoa Lightning Talk

Auto-layout : Managing Frames ReactiveCocoa : Imperative programming

Page 10: Reactive Cocoa Lightning Talk

Complexity Is

STATE

Page 11: Reactive Cocoa Lightning Talk

BOOL loadingNextPageOfComments;

BOOL commentsAreShowing;

BOOL commentsLoadingShowing;

BOOL flippingAnswers;

BOOL isOwnerOfQuestion;

2 states

4 states

8 states

16 states

32 states

Page 12: Reactive Cocoa Lightning Talk

- (void) updateChangeAnswerButtonText { if (self.didAnswerQuestion && self.inResultsView) { [self.commentHeaderCell configureWithStyle:TCQuestionDetailCommentHeaderCellChangeAnswer]; } else if (self.isOwnerOfQuestion && !self.didAnswerQuestion && self.inResultsView) { [self.commentHeaderCell configureWithStyle:TCQuestionDetailCommentHeaderCellViewAnswers]; } else { [self.commentHeaderCell configureWithStyle:TCQuestionDetailCommentHeaderCellViewStats]; } } - (void) updateViewCommentStatus { if ((self.didAnswerQuestion || self.isOwnerOfQuestion) && !self.commentsAreShowing) { self.commentsAreShowing = YES; self.commentsLoadingShowing = YES; … } [self showCommentBox:self.inResultsView || self.isOwnerOfQuestion]; } !^aBlock { sself.didAnswerQuestion = sself.question.userResponse ? YES : NO; sself.inResultsView = sself.didAnswerQuestion || self.isOwnerOfQuestion;

if (sself.questionFullyLoaded) { return; } …

}

Real code from one file

- (void) prepareForReuse { self.state = TCQuestionDetailViewControllerStateInactive; self.questionFullyLoaded = NO; self.isOwnerOfQuestion = NO; self.didAnswerQuestion = NO; [self showCommentBox:NO]; self.loadingNextPageOfComments = NO; self.commentsAreShowing = NO; self.commentsLoadingShowing = NO; }

Page 13: Reactive Cocoa Lightning Talk

ReactiveCocoa to the Rescue!

• Signals instead of mutable variables

• RACSignal – Think of it as a beacon sending out new values to it’s subscribers

Page 14: Reactive Cocoa Lightning Talk

RACSignal

• A unified interface for observing and reacting to all kinds of events and their values• UI Action• Async Network Call• KVO• Async Processing

It’s all input!

Page 15: Reactive Cocoa Lightning Talk

RACSignal• Shared vocabulary for transforming the received values

• Combine, filter, reduce, map, and many more operations

• Eventually generate output (side effects)

• Updating a label

• Update an array with objects from the network

• etc.

Page 16: Reactive Cocoa Lightning Talk

- (void)viewDidLoad { [super viewDidLoad];

[self.username addTarget:self action:@selector(textFieldTextDidChange:) forControlEvents:UIControlEventAllEditingEvents];

[self.email addTarget:self action:@selector(textFieldTextDidChange:) forControlEvents:UIControlEventAllEditingEvents];} !- (void)textFieldTextDidChange:(UITextField *)field { BOOL validUsername = self.username.text.length > 0; NSRange at = [self.email.text rangeOfString:@"@"]; BOOL validEmail = at.location != NSNotFound; self.signupButton.enabled = validUsername && validEmail;}

Your name

Your email address

Sign Up

Solved the imperative way.

signUpButton.enabled

Old

Page 17: Reactive Cocoa Lightning Talk

RAC(self.signUpButton, enabled) = [RACSignal combineLatest:@[ self.name.rac_textSignal, self.email.rac_textSignal ] reduce:^(NSString *name, NSString *email) { NSRange at = [email rangeOfString:@"@"]; return @(at.location != NSNotFound && name.length > 0); }];

Your name

Your email address

Sign Up

Solved the reactive way.

Should I be enabled?

AWESOME

Page 18: Reactive Cocoa Lightning Talk

RAC(self.signUpButton, enabled) = [RACSignal combineLatest:@[ self.name.rac_textSignal, self.email.rac_textSignal ] reduce:^(NSString *name, NSString *email) { NSRange at = [email rangeOfString:@"@"]; return @(at.location != NSNotFound && name.length > 0); }];

Your name

Your email address

Sign Up

Solved the reactive way.

Should I be enabled?

AWESOME

Page 19: Reactive Cocoa Lightning Talk

RACSignal *validateEmailSignal = [self.email.rac_textSignal map:^id(NSString *emailString) { return [MyAPI validatedEmail:emailString]; }]; !RAC(self.signUpButton, enabled) = [RACSignal combineLatest:@[ self.name.rac_textSignal, validateEmailSignal ] reduce:^(NSString *name, APIResponse *response) { return @(response.json.isValid == YES && name.length > 0); }];

Solved the reactive way.AWESOME

Your name

Your email address

Sign Up

Should I be enabled?

Network Validation? Easy.

Page 20: Reactive Cocoa Lightning Talk

RACSignal *validateEmailSignal = map }];!RAC combineLatest ] reduce }];

Solved the AWESOME

Your name

Your email address

Sign Up

Should I be enabled?

Network Validation? Easy.

The point is to Minimize State

Page 21: Reactive Cocoa Lightning Talk

RACSignal *validateEmailSignal = map }];!RAC combineLatest ] reduce }];

Solved the AWESOME

Your name

Your email address

Sign Up

Should I be enabled?

Network Validation? Easy.

The point is to Minimize State

Page 22: Reactive Cocoa Lightning Talk

Check out ReactiveCocoa Your Brain Will Thank You

https://github.com/ReactiveCocoa/ReactiveCocoa

Bob Spryn LoHi Labs @sprynmr (github, twitter) !Thanks to @jspahrsummers, @joshaber, @AshFurrow, @robb, @andrewsardone and @erikprice (github usernames) for sharing their presentation slides and helping me learn ReactiveCocoa