Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Basic introduction to Reactive Cocoa

A Basic introduction to Reactive Cocoa

Presentation i gave about reactive cocoa in cocoaheads bangalore.

hari-tw

July 06, 2013
Tweet

Other Decks in Programming

Transcript

  1. var x = 10; var y = 5; var z

    = x + y; x = 20; assert.areEqual(25, z); Monday, 8 July 13
  2. var x = 10; var y = 5; var z

    = x + y; x = 20; assert.areEqual(25, z); Monday, 8 July 13
  3. imperative programming var x = 10; var y = 5;

    var z = x + y; x = 20; assert.areEqual(25, z); Monday, 8 July 13
  4. var x := 10; var y := 5; var z

    := x + y; x := 20; assert.areEqual(25, z); Monday, 8 July 13
  5. var x := 10; var y := 5; var z

    := x + y; x := 20; assert.areEqual(25, z); Monday, 8 July 13
  6. reactive programming var x := 10; var y := 5;

    var z := x + y; x := 20; assert.areEqual(25, z); Monday, 8 July 13
  7. imperative programming • tell the “machine” how to do something

    and as a result what you want to happen will happen Monday, 8 July 13
  8. imperative programming • tell the “machine” how to do something

    and as a result what you want to happen will happen NSArray *names = @[@"apple", @"google", @"microsoft"]; NSMutableArray *decoratedNames = [NSMutableArray array]; for (int i = 0; i < names.count; i++ ) { decoratedNames[i] = [names[i] uppercaseString]; } // decoratedNames: @[ @"APPLE", @"GOOGLE", @"MICROSOFT" ] Monday, 8 July 13
  9. declarative programming • tell the “machine” what you would like

    to happen, and let the machine figure out how to do it Monday, 8 July 13
  10. declarative programming • tell the “machine” what you would like

    to happen, and let the machine figure out how to do it id *decoratedNames = [@[@"apple", @"google", @"microsoft"] ! ! ! ! ! map:^id(NSString *name){ ! ! ! ! ! ! return [name uppercaseString]; ! ! ! ! ! }]; // decoratedNames: @[ @"APPLE", @"GOOGLE", @"MICROSOFT" ] Monday, 8 July 13
  11. so what? • Increases the complexity • maintaining lots of

    states • unwanted templates/boilerplate codes (delegates/protocols/KVO/notofications etc) • poor code locality (where does this code belong) Monday, 8 July 13
  12. streams • sequence of values • values over variables •

    no in-place modification Monday, 8 July 13
  13. signals • observes the streams • reacts to the past,

    present & future values • similar to promises / futures Monday, 8 July 13
  14. trace our test x y z:= x+y 10 x 5

    15 Monday, 8 July 13
  15. trace our test x y z:= x+y 10 x 5

    20 15 Monday, 8 July 13
  16. trace our test x y z:= x+y 10 x 5

    20 15 Monday, 8 July 13
  17. trace our test x y z:= x+y 10 x 5

    20 15 30 Monday, 8 July 13
  18. trace our test x y z:= x+y 10 x 20

    5 20 15 30 Monday, 8 July 13
  19. trace our test x y z:= x+y 10 x 20

    5 20 x 15 30 Monday, 8 July 13
  20. trace our test x y z:= x+y 10 x 20

    5 20 x 15 30 Monday, 8 July 13
  21. trace our test x y z:= x+y 10 x 20

    5 20 x 15 30 40 Monday, 8 July 13
  22. trace our test x y z:= x+y 10 x 20

    25 5 20 x 15 30 40 Monday, 8 July 13
  23. trace our test x y z:= x+y 10 x 20

    25 5 20 x 25 15 30 40 Monday, 8 July 13
  24. trace our test x y z:= x+y 10 x 20

    25 5 20 x 25 15 30 40 Monday, 8 July 13
  25. trace our test x y z:= x+y 10 x 20

    25 5 20 x 25 15 30 40 50 Monday, 8 July 13
  26. trace our test x y z:= x+y 10 x 20

    25 x 5 20 x 25 15 30 40 50 Monday, 8 July 13
  27. trace our test x y z:= x+y 10 x 20

    25 x 5 20 x 25 x 15 30 40 50 Monday, 8 July 13
  28. trace our test x y z:= x+y 10 x 20

    25 x 5 20 x 25 x 15 30 40 50 Monday, 8 July 13
  29. trace our test x y z:= x+y 10 x 20

    25 x 5 20 x 25 x 15 30 40 50 x Monday, 8 July 13
  30. 3 key components • RACSignal • sends a sequence of

    events with some data • RACSubscriber • receives from signals • RACDisposable • encapsulates cleanup and deallocation Monday, 8 July 13
  31. RACSignal • n (arbitrary length of next) events sending data

    • multiple values can be sent a tuples • completion and an error event Monday, 8 July 13
  32. RACSubscriber • subscribe to events sent by RACSignal • a

    protocol that can be implemented anywhere Monday, 8 July 13
  33. RACSubscriber • subscribe to events sent by RACSignal • a

    protocol that can be implemented anywhere [RACSignal someSignal] subscribeNext:^(id x) { NSLog(@"Next event : %@", x);} completed : ^{ NSLog(@"done!");} error : ^(NSError *error){ NSLog(@"Error : %@", error); }]; Monday, 8 July 13
  34. Bridges • RACAble() Macro - Signals from KVO • RAC()

    Macro - Subscribes from Key Path • RACSubject - Mutable signal • -rac_lift_selector - Make signals from method invocations • -rac_signalForControlEvents - Signals from UIControl action • -rac_addObserverForName - Signals from Notification center Monday, 8 July 13
  35. when to use • Handling asynchronous events • chaining dependent

    events • parallelizing independent work • simplifying collections transformations Monday, 8 July 13