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

Getting Started with ReactiveCocoa v4.1.0

Getting Started with ReactiveCocoa v4.1.0

Eac0bf787b5279aca5e699ece096956e?s=128

Yasuhiro Inami

April 13, 2016
Tweet

Transcript

  1. Getting Started with ReactiveCocoa v4.1.0 2016/04/13 Reactive Swift Meetup Yasuhiro

    Inami / @inamiy
  2. Reactive Programming

  3. None
  4. Streams of Events over time

  5. ReactiveCocoa (ObjC/Swift)

  6. Signal (Hot Observable) SignalProducer (Cold Observable)

  7. ‗ and ❄

  8. None
  9. Hot & Cold Observables • Hot Observable ‗ • always

    active (may lose data if not subscribed) • uses same underlying source (broadcasting) • Cold Observable ❄ • paused until subscription • clones underlying source (not broadcasting)
  10. None
  11. In short...

  12. SignalProducer ↯ Signal ↯ Event

  13. SignalProducer ↯ Signal ↓ Observer

  14. SignalProducer ↯ Signal ↓ɹɹˣ ObserverɹObserver

  15. SignalProducer ↯    ↯ SignalɹSignal ↓ɹɹɹɹˣ ObserverɹObserver

  16. Function Composition

  17. None
  18. Signal -> Signal SignalProducer -> SignalProducer

  19. Signal Operations • Transforming: map, mapError, scan, reduce, ... •

    Filtering: filter, take, takeUntil, skip, sampleOn, ... • Combining: flatten (Merge/Concat/Latest), combineLatest, zip, flatMap, flatMapError, ... • Timing: delay, throttle, timeoutWithError, ... • Utility: observeOn, materialize, dematerialize, ...
  20. SignalProducer Operations • Transforming: map, mapError, scan, reduce, ... •

    Filtering: filter, take, takeUntil, skip, sampleOn, ... • Combining: flatten (Merge/Concat/Latest), combineLatest, zip, flatMap, flatMapError, ... • Timing: delay, throttle, timeoutWithError, ... • Utility: observeOn, materialize, dematerialize, ... • Producer-Only: startOn, concat, retry, etc...
  21. Why do we need 2 different types?

  22. Because ‗ and ❄ behaviors differ!

  23. Define types based on its behavior (as simple as possible)

  24. Signal / SignalProducer • Signal • Container for observers (for

    broadcasting) • .pipe = pair of (Signal, Observer) = Subject • SignalProducer • Container for startHandler (for cloning) • .buffer = pair of (SignalProducer, Observer) = ReplaySubject
  25. Other data types • Observer: Wrapper of Event -> ()

    • Event: .Next, .Failed, .Completed, .Interrupted • Scheduler: GCD / NSThread wrapper • Disposable: Stateful container of () -> () • Property: Latest value + Signal(Producer) • Action: Stateful container of Input -> SignalProducer
  26. Compared to other FRPs... • Protocol-oriented, no abstract classes &

    subclassing • Functional, much less types to learn • Separation of Signal & SignalProducer • No shareReplay, refCount (ConnectableObservable) • Typed errors • combineLatest(o1, o2, ...) ... no mixed error types • property <~ signal ... only NoError is allowed
  27. Demo inamiy/ReactiveCocoaCatalog

  28. Recap • ReactiveCocoa has separated hot & cold observables •

    Signal • SignalProducer • Type safe, including generic Error type • Use RAC if you like Result<T, E> more than Result<T> • RAC is beautiful !✨
  29. Thanks!