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
Tweet

More Decks by Swift India

Other Decks in Technology

Transcript

  1. 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
  2. Rx Programming • Observer Pattern (publish >> observe) • Operations

    (dependent tasks) • Simple (yet powerful) Operators like merge, zip, debounce… • Open Source: ReactiveSwift, RxSwift
  3. Why Rx? • multiple async tasks • better state management

    • loose coupling of components • declarative & cleaner api
  4. 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
  5. • Notification Publisher • URLSession Publisher • Just, Future •

    CurrentValueSubject, PassthroughSubject Free Publishers
  6. 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
  7. • `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
  8. • pre-defined functions (publishers) • used b/w a publisher &

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

  10. Zip