Combine! All the things

Combine! All the things

8d05c774222cd7ee18ea73005ff37a55?s=128

Ritesh Gupta

July 28, 2019
Tweet

Transcript

  1. Combine! All the things Ritesh Gupta @_riteshhh

  2. Ritesh Gupta @_riteshhh riteshhh.com github.com/riteshhgupta

  3. None
  4. Beta 1, 2, 3, 4…!

  5. Combine • It provides a declarative Swift API for processing

    values over time • These values can represent many kinds of asynchronous events • Customise handling of asynchronous events by combining event-processing operators
  6. • It’s a reactive framework by Apple Combine

  7. Reactive (Rx) Programming

  8. Rx Programming • Observer Pattern (publish >> observe) • Operations

    (dependent tasks) • Simple (yet powerful) Operators like merge, zip, debounce… • Open Source: ReactiveSwift, RxSwift
  9. Why we need Rx?

  10. Why Rx? • multiple async tasks • better state management

    • loose coupling of components • declarative & cleaner api
  11. source: https://twitter.com/diegopetrucci/status/1135655480825655297 Current state of Combine

  12. Foundation of Combine!

  13. Foundation of Combine! • Publisher • Subscriber • Operators (map,

    flatMap, filter, merge, zip…)
  14. Publisher

  15. Publisher • emit sequence of values over time • delivers

    values to one or more Subscriber instances • protocol with two generic types (Output, Error) • completion via .finished or .failure
  16. Publisher

  17. • Notification Publisher • URLSession Publisher • Just, Future •

    CurrentValueSubject, PassthroughSubject Free Publishers
  18. Subscriber

  19. Subscriber • receive emitted values from a Publisher • represents

    end of the chain of a stream • protocol with two generic types (Input, Error) • can cancel a subscription • Sink, Assign, AnySubscriber
  20. Subscriber

  21. Publisher Subscriber

  22. Subscription

  23. Assign

  24. Assign

  25. Sink

  26. Sink

  27. subscribe(on:) vs receive(on:)

  28. Threading

  29. • `receive(on:)` affects downstream events • `subscribe(on:)` affects upstream events

    • In the example, requests to `$queryPublisher` are performed on `backgroundQueue`, but elements received from it are performed on `Dispatch.main` Threading
  30. Lifecycle of Publisher & Subscriber

  31. Lifecycle

  32. Operators

  33. • pre-defined functions (publishers) • used b/w a publisher &

    a subscriber • transform the stream • map, flatMap, filter, merge, zip, debounce… Operators
  34. Operators

  35. Map

  36. Filter

  37. Debounce

  38. FlatMap

  39. Merge

  40. Zip

  41. Demo

  42. Next?

  43. • Subject • Memory Management • Error Handling • Debugging

    Next?
  44. • ConnectablePublisher • Back Pressure (`Demand`) • Multicasts Next?

  45. Further Reading

  46. Thank you!