Your SlideShare is downloading. ×
  • Like
Reactive cocoa
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Reactive cocoa

  • 722 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
722
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
17
Comments
0
Likes
10

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ReactiveCocoa joehsieh 2014/02/13
  • 2. Outline • What is reactiveCocoa? • RAC basic components • MVVM
  • 3. What is ReactiveCocoa? • ReactiveCocoa = Functional Reactive(Binding) Programming
  • 4. MVC ViewController exists many glue code.
  • 5. Cocoa Binding(Mac Only) • The Cocoa bindings technology provides a means of keeping model and view values synchronized without you having to write a lot of “glue code.”(less glue code) It allows you to establish a mediated connection between a view and a piece of data, “binding” them such that a change in one is reflected in the other(reactive).
  • 6. Demo for Cocoa Binding
  • 7. Binding for iOS? • Binding (Reactive) version(Mac Only) is based on KVO, so we can also achieve binding by KVO in iOS.
  • 8. KVO Demo
  • 9. RAC basic components
  • 10. RACStream • It applies higher-oder functions to handle incoming data (map, reduce, filter, ect…) • for binding values : RACSignal(Push-driven) • for collection : RACSequence(Pull-driven)
  • 11. RACSignal filter subject v map v observer ! • value: nextValue, error or complete message.
  • 12. KVO [RACObserve(self, username) subscribeNext:^(NSString *newName) {! NSLog(@"%@", newName);! }];
  • 13. Chained Signal [[RACObserve(self, username)! filter:^(NSString *newName) {! return [newName hasPrefix:@"j"];! }]! subscribeNext:^(NSString *newName) {! NSLog(@"%@", newName);! }];
  • 14. Derive state RAC(self, createEnabled) = [RACSignal ! combineLatest:@[ RACObserve(self, password), RACObserve(self, passwordConfirmation) ] ! reduce:^(NSString *password, NSString *passwordConfirm) { return @([passwordConfirm isEqualToString:password]); }];
  • 15. Trigged after a group of operations completes [[RACSignal ! merge:@[ [client fetchUserRepos], [client fetchOrgRepos] ]] ! subscribeCompleted:^{! NSLog(@"They're both done!");! }];
  • 16. Solves callback hell [[[[client ! logInUser] ! flattenMap:^(User *user) {! // Return a signal that loads cached messages for the user.! return [client loadCachedMessagesForUser:user];! }]! flattenMap:^(NSArray *messages) {! // Return a signal that fetches any remaining messages.! return [client fetchMessagesAfterMessage:messages.lastObject];! }]! subscribeNext:^(NSArray *newMessages) {! NSLog(@"New messages: %@", newMessages);! } completed:^{! NSLog(@"Fetched all messages.");! }];
  • 17. Advantages • RAC is that it provides a single, unified approach to dealing with asynchronous behaviors, including delegate methods, callback blocks, target-action mechanisms, notifications, and KVO.
  • 18. RACSequence • Adds higher-order functions to collection. • It is pull-driven(works on demand)
  • 19. Imperative Programming testData is an array from 1 to 100 NSMutableArray *result = [NSMutableArray array]; for (NSNumber *i in testData) { NSUInteger tempValue = @(pow([i intValue] , 2)); if (tempValue % 2 == 0) { [result addObject:@(tempValue)]; } }
  • 20. Functional Programming RACSequence *sequence = [[[testData rac_sequence] map:^id(id value) { return @(pow([value intValue] , 2)); }] filter:^BOOL(id value) { return ([value intValue] % 2 == 0); }]; // evaluate sequence (pull data from sequence) NSLog(@"%@", [sequence array]);
  • 21. Demo
  • 22. BMI Imperative
  • 23. BMI RAC height text field 180cm weight text field height weight 75kg combine Input BMIValue label 23.15 BMIValue Output map BMIStatus label Normal weight green BMIStaus background color
  • 24. Refines BMI by MVVM Separates presentation and validation logic from controller to viewModel.
  • 25. Benefits of MVVM • We can easily Unit Test the View Model • It’s easier to change the UI
  • 26. Reference • http://elm-lang.org/learn/What-is-FRP.elm • http://blog.maybeapps.com/post/42894317939/input-and-output • https://github.com/ReactiveCocoa/ReactiveCocoa • http://www.teehanlax.com/blog/model-view-viewmodel-for-ios/ • https://leanpub.com/iosfrp • http://blog.devtang.com/blog/2014/02/11/reactivecocoaintroduction/ • http://github.com/joehsieh/RACBMIiOS <— here is my demo