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

Swift India Conf 2019: Combine! All the things

Swift India Conf 2019: Combine! All the things

Speaker: Ritesh Gupta, iOS Engineer, Over Inc

Twitter: https://twitter.com/_riteshhh/

Bio: Ritesh Gupta is an iOS Engineer at Over. He believes in protocol-oriented & reactive programming and also enjoys writing about them on medium. He also had the honor of writing an issue of iOS dev weekly & has given multiple talks on various meetups/conferences. When he is not coding, you can find him exploring Indian fusion cuisines.

Abstract: Recently in WWDC'19, Apple has announced their reactive framework called Combine and I couldn't be more excited as I have been tinkering with reactive programming for the last 4 years now. In this talk, we will go through the basics of reactive programming and gradually dive into how we can use Combine through a demo app. In this journey, we will explore various concepts like Publisher, Subscriber, Subject along with some of the operators like map, flatmap, filter, merge, zip, etc through real-life examples.


Swift India

July 28, 2019


  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

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

  45. Further Reading

  46. Thank you!