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

Combine! All the things

Combine! All the things

Ritesh Gupta

July 28, 2019
Tweet

More Decks by Ritesh Gupta

Other Decks in Programming

Transcript

  1. Combine! All the things
    Ritesh Gupta
    @_riteshhh

    View Slide

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

    View Slide

  3. View Slide

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

    View Slide

  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

    View Slide

  6. • It’s a reactive framework by Apple
    Combine

    View Slide

  7. Reactive (Rx) Programming

    View Slide

  8. Rx Programming
    • Observer Pattern (publish >> observe)
    • Operations (dependent tasks)
    • Simple (yet powerful) Operators like merge, zip, debounce…
    • Open Source: ReactiveSwift, RxSwift

    View Slide

  9. Why we need Rx?

    View Slide

  10. Why Rx?
    • multiple async tasks
    • better state management
    • loose coupling of components
    • declarative & cleaner api

    View Slide

  11. source: https://twitter.com/diegopetrucci/status/1135655480825655297
    Current state of Combine

    View Slide

  12. Foundation of Combine!

    View Slide

  13. Foundation of Combine!
    • Publisher
    • Subscriber
    • Operators (map, flatMap, filter, merge, zip…)

    View Slide

  14. Publisher

    View Slide

  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

    View Slide

  16. Publisher

    View Slide

  17. • Notification Publisher
    • URLSession Publisher
    • Just, Future
    • CurrentValueSubject, PassthroughSubject
    Free Publishers

    View Slide

  18. Subscriber

    View Slide

  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

    View Slide

  20. Subscriber

    View Slide

  21. Publisher
    Subscriber

    View Slide

  22. Subscription

    View Slide

  23. Assign

    View Slide

  24. Assign

    View Slide

  25. Sink

    View Slide

  26. Sink

    View Slide

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

    View Slide

  28. Threading

    View Slide

  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

    View Slide

  30. Lifecycle of Publisher &
    Subscriber

    View Slide

  31. Lifecycle

    View Slide

  32. Operators

    View Slide

  33. • pre-defined functions (publishers)
    • used b/w a publisher & a subscriber
    • transform the stream
    • map, flatMap, filter, merge, zip, debounce…
    Operators

    View Slide

  34. Operators

    View Slide

  35. Map

    View Slide

  36. Filter

    View Slide

  37. Debounce

    View Slide

  38. FlatMap

    View Slide

  39. Merge

    View Slide

  40. Zip

    View Slide

  41. Demo

    View Slide

  42. Next?

    View Slide

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

    View Slide

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

    View Slide

  45. Further Reading

    View Slide

  46. Thank you!

    View Slide