Successfully reported this slideshow.
ReactiveCocoa
Bob Spryn
LoHi Labs
@sprynmr (github, twitter)
!
!
!

https://github.com/ReactiveCocoa/ReactiveCocoa
ReactiveCocoa
•

An implementation of functional reactive programming

•

It provides APIs for composing and transforming
...
?
Not just another API.

It enables you to

Program Smarter
Focus on WHAT not HOW
Reactive!!! Declarative!!!

What does that even mean?
• Setup

your reactions

• Compose,

Reactive!!! Declarative!!!

split, filter, etc.

What does that even mean?
Imperative
The way we normally program.
Imperative
The way we normally program.

•

Observe, wait, then command & control

•

Dealing with the HOW, not just the W...
Auto-layout : Managing Frames
ReactiveCocoa : Imperative programming
Complexity
Is
STATE
BOOL loadingNextPageOfComments;

2 states


BOOL commentsAreShowing;

4 states


BOOL commentsLoadingShowing;

8 states


...
- (void) updateChangeAnswerButtonText {
if (self.didAnswerQuestion && self.inResultsView) {
[self.commentHeaderCell config...
ReactiveCocoa to the Rescue!

•

Signals instead of mutable variables


•

RACSignal – Think of it as a beacon sending out...
RACSignal
•

A unified interface for observing and reacting to all kinds of events
and their values

•

UI Action


•

Asyn...
RACSignal
•

Shared vocabulary for transforming the received values

•

•

Combine, filter, reduce, map, and many more oper...
- (void)viewDidLoad {

[super viewDidLoad];

[self.username

addTarget:self

action:@selector(textFieldTextDidChange:)

fo...
Your name
Your email address
RAC(self.signUpButton, enabled) = [RACSignal
combineLatest:@[

self.name.rac_textSignal,

sel...
Your name
Your email address
RAC(self.signUpButton, enabled) = [RACSignal
combineLatest:@[

self.name.rac_textSignal,

sel...
Network Validation?
Easy.

Your name
Your email address
Sign Up

RACSignal *validateEmailSignal = [self.email.rac_textSign...
Network Validation?
Easy.

Your name
Your email address
Sign Up

RACSignal *validateEmailSignal =
map
}];
!

uld I
Sho

Th...
Network Validation?
Easy.

Your name
Your email address
Sign Up

RACSignal *validateEmailSignal =
map
}];
!

uld I
Sho

Th...
Check out ReactiveCocoa
Your Brain Will Thank You
https://github.com/ReactiveCocoa/ReactiveCocoa

Bob Spryn
LoHi Labs
@spr...
Upcoming SlideShare
Loading in …5
×

Reactive Cocoa Lightning Talk

1,766 views

Published on

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

Published in: Technology, Business
  • Be the first to comment

Reactive Cocoa Lightning Talk

  1. 1. ReactiveCocoa Bob Spryn LoHi Labs @sprynmr (github, twitter) ! ! ! https://github.com/ReactiveCocoa/ReactiveCocoa
  2. 2. 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
  3. 3. ?
  4. 4. Not just another API. It enables you to Program Smarter Focus on WHAT not HOW
  5. 5. Reactive!!! Declarative!!! What does that even mean?
  6. 6. • Setup your reactions • Compose, Reactive!!! Declarative!!! split, filter, etc. What does that even mean?
  7. 7. Imperative The way we normally program.
  8. 8. Imperative The 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
  9. 9. Auto-layout : Managing Frames ReactiveCocoa : Imperative programming
  10. 10. Complexity Is STATE
  11. 11. BOOL loadingNextPageOfComments; 2 states BOOL commentsAreShowing; 4 states BOOL commentsLoadingShowing; 8 states BOOL flippingAnswers; 16 states BOOL isOwnerOfQuestion; 32 states
  12. 12. - (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; - (void) prepareForReuse { self.commentsLoadingShowing = YES; self.state = TCQuestionDetailViewContro … self.questionFullyLoaded = NO; } self.isOwnerOfQuestion = NO; self.didAnswerQuestion = NO; [self showCommentBox:self.inResultsView || self.isOwnerOfQuestion]; [self showCommentBox:NO]; } self.loadingNextPageOfComments = NO; self.commentsAreShowing = NO; self.commentsLoadingShowing = NO; ! ^aBlock { sself.didAnswerQuestion = sself.question.userResponse ? YES : NO; sself.inResultsView = sself.didAnswerQuestion || self.isOwnerOfQuestion; if (sself.questionFullyLoaded) { return; } … } } l co Rea rom ef d file one
  13. 13. ReactiveCocoa to the Rescue! • Signals instead of mutable variables • RACSignal – Think of it as a beacon sending out new values to it’s subscribers
  14. 14. RACSignal • A unified interface for observing and reacting to all kinds of events and their values • UI Action • Async Network Call • KVO • Async Processing t’s I in ll a t! pu
  15. 15. 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.
  16. 16. - (void)viewDidLoad {
 [super viewDidLoad];
 [self.username
 addTarget:self
 action:@selector(textFieldTextDidChange:)
 forControlEvents:UIControlEventAllEditingEvents];
 [self.email
 addTarget:self
 action:@selector(textFieldTextDidChange:)
 forControlEvents:UIControlEventAllEditingEvents];
 Your name Your email address Sign Up abled ton.en ut gnUpB si } ! - (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;
 } Old Solved the imperative way.
  17. 17. Your name Your email address 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);
 }]; Sign Up ould I Sh SOME AWE bled? e ena b Solved the reactive way.
  18. 18. Your name Your email address 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);
 }]; Sign Up ould I Sh SOME AWE bled? e ena b Solved the reactive way.
  19. 19. Network Validation? Easy. Your name Your email address Sign Up RACSignal *validateEmailSignal = [self.email.rac_textSignal map:^id(NSString *emailString) { return [MyAPI validatedEmail:emailString]; }]; uld I Sho bled? be ena ! RAC(self.signUpButton, enabled) = [RACSignal combineLatest:@[
 self.name.rac_textSignal,
 validateEmailSignal
 ]
 reduce:^(NSString *name, APIResponse *response) {
 return @(response.json.isValid == YES && name.length > 0);
 }]; SOME AWE Solved the reactive way.
  20. 20. Network Validation? Easy. Your name Your email address Sign Up RACSignal *validateEmailSignal = map }]; ! uld I Sho The point is to Minimize State RAC combineLatest ] reduce }]; Solved the SOME AWE bled? be ena
  21. 21. Network Validation? Easy. Your name Your email address Sign Up RACSignal *validateEmailSignal = map }]; ! uld I Sho The point is to Minimize State RAC combineLatest ] reduce }]; Solved the SOME AWE bled? be ena
  22. 22. 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

×