Reactive Cocoa

2,091 views

Published on

The basics of Reactive Cocoa. The tips and tricks in this presentation will cover almost all the use cases for Reactive Cocoa. Demo here: https://github.com/rob-brown/Demos/tree/master/RACDemo.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,091
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Reactive Cocoa

  1. 1. Reactive Cocoa Robert Brown Twitter: @robby_brown ADN: @robert_brown
  2. 2. What is Reactive Cocoa? A framework for Functional Reactive Programing (FRP) Developed by GitHub
  3. 3. What is Functional Reactive Programming?
  4. 4. –Wikipedia “Functional reactive programming (FRP) is a programming paradigm for reactive programming using the building blocks of functional programming.”
  5. 5. What are the building blocks of functional programming? ! reduce/fold map filter zip ! drop take concat and more
  6. 6. What is functional programming? Immutable data Limited state Pure functions
  7. 7. Immutable Data Data structures can’t be modified in place Values are copied as needed NSArray vs. NSMutableArray
  8. 8. Limited State No global data No side effects
  9. 9. Pure Functions Given input X, the function always returns Y Allows for many compiler-level optimizations
  10. 10. Higher Order Functions
  11. 11. Map 1 2 3 2 4 6 x * 2 5 6 7 x + 4
  12. 12. Reduce / Fold 1 2 3 0 Sum 136
  13. 13. How do I use Reactive Cocoa?
  14. 14. RACSignal RACSignal is a stream of data, possibly infinite Its values are evaluated lazily Signals are the most used objects in Reactive Cocoa
  15. 15. RAC as KVO Advantages: No more hard-coded key paths No more comparing key paths Code locality Refactoring will update RAC bindings
  16. 16. RAC as KVO Disadvantages: Watch out for retain cycles All objects are of type id
  17. 17. RAC as KVO [RACObserve(self, name) subscribeNext: ^(NSString * name) { NSLog(@“Name changed: %@”, name); }];
  18. 18. RAC Bindings Like Cocoa Bindings Automatically changes properties
  19. 19. RAC Bindings RAC(self, textField2.text) = self.textField1.rac_textSignal; ! RAC(self, nameField.text) = RACObserve(self, name);
  20. 20. Tips and Tricks Transform the values of a signal: [signal map:^id(NSString * text) { return [text uppercaseString]; }];
  21. 21. Tips and Tricks Combine the results of many signals: [RACSignal combineLatest:@[s1, s2] reduce: ^id(NSNumber * b1, NSNumber * b2) { return @([b1 boolValue] && [b2 boolValue]); }];
  22. 22. Tips and Tricks Watch for when multiple tasks complete: [[RACSignal merge:@[signal1, signal2] subscribeCompleted:^{ NSLog(@“All done”); }];
  23. 23. Tips and Tricks Add actions to buttons: self.button.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id x) { // Code goes here };
  24. 24. Tips and Tricks Create arbitrary signals: [RACSignal createSignal: ^RACDisposable *(id<RACSubscriber> subscriber) { // Send next, error, and completed // messages to subscriber }];
  25. 25. Tips and Tricks Deliver signal results to the main thread: [backgroundSignal deliverOn: [RACScheduler mainThreadScheduler]];
  26. 26. Tips and Tricks If you need to perform side effects: [signal doNext:^(id value) {…}]; [signal doError:^(NSError * error) {…}]; [signal doCompleted:^{…}];
  27. 27. Tips and Tricks Limit the rate a signal can be evaluated: [signal throttle:10.0];
  28. 28. Questions?
  29. 29. Demo
  30. 30. Want to Learn More? GitHub NSHipster Ray Wenderlich
  31. 31. Other Implementations C♯ Reactive Extensions (Rx) JavaScript react.js

×