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

Combine - Diving into Apple’s Reactive Programming

Combine - Diving into Apple’s Reactive Programming

Swift Hyderabad Chapter 5
Speaker: Ankush Bhatia

Abstract: At WWDC 2019, Apple introduced their own version of reactive framework which can be a new approach for working with asynchronous code. We will be exploring fundamentals of Combine with its application.

Bio: Sr. iOS Engineer @ Mutual Mobile. He has been working on Swift language from the day it came into this 🌎.
Linkedin: https://www.linkedin.com/in/ankush-bhatia-ios

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

January 25, 2020
Tweet

Transcript

  1. COMBINE DIVING INTO APPLE’S REACTIVE PROGRAMMING

  2. ANKUSH BHATIA Sr. iOS Engineer @Mutual Mobile Twitter: @ankush1419 Github:

    ankush-bhatia Medium: @ankush_bhatia Linkedin: ankush-bhatia-ios Web: ankushbhatia.com
  3. COMBINE INTRO

  4. COMBINE INTRO

  5. COMBINE EXISTING ASYNCHRONOUS APIS ▸ Notification Center ▸ Delegates ▸

    GCD / Operation Queues ▸ Closures
  6. COMBINE PROBLEMS WITH IMPERATIVE ASYNC PROGRAMMING ▸ Order of execution

    ▸ State management ▸ Different APIs to execute asynchronous code Task 1 Task 2 Task 3 Task 4 Tasks Time
  7. COMBINE WHAT IS COMBINE? ▸ Declarative API for processing values

    over time. ▸ Built for handling asynchronous events easily. ▸ Alternate approach. ▸ Type Safe ▸ Only Available from iOS 13.0 and mac OS Catalina or later.
  8. COMBINE PILLARS OF COMBINE COMBINE PUBLISHER OPERATOR SUBSCRIBER

  9. COMBINE PUBLISHERS ▸ Publishers emits values over time to the

    interested parties. ▸ It can do two type of things: ▸ Send generic output of Output type. It can be anything from value to object. ▸ Can send completion of success or failure. ▸ Publisher do not emit any values if no one is there to listen. (no subscribers currently subscribed to publisher.)
  10. COMBINE PUBLISHER PROTOCOL

  11. COMBINE SUBSCRIBERS ▸ Subscribers are the ones which act upon

    the data received from the publishers. ▸ Combine provides us two built in subscribers. ▸ Sink: Allows to get callback of the data in the completion block. ▸ Assign: Allows to assign output to some property on data model or UI view to update.
  12. COMBINE SUBSCRIBER PROTOCOL

  13. COMBINE OPERATORS ▸ These are something which can be called

    as middleware which act upon existing publishers and provides a new publisher. ▸ Order of the operators matter as the Output of one should match with the input of the another. ▸ Upstream: Input of the operator. ▸ Downstream: Output of the operator. ▸ Uses: ▸ Functional Transformations (Map, Reduce, Filter) ▸ Thread handling. ▸ Error handling.
  14. COMBINE TYPE OF OPERATORS Source: WWDC Slides

  15. COMBINE LIFE CYCLE 1. Subscriber subscribes to publisher. 2. Publisher

    sends subscriptions to subscriber. 3. Subscriber uses subscription to request values from publisher. 4. Publisher sends values as requested by subscriber. 5. Publisher sends completion or failure to end subscription. WWDC Slides
  16. COMBINE IS IT REALLY JUST AN ALTERNATE APPROACH?

  17. COMBINE MORE TO COMBINE ▸ Memory Management ▸ Cancellable Protocol

    ▸ [AnyCancellable] ▸ Testability
  18. COMBINE ERROR HANDLING ▸ assertNoFailure() ▸ catch() ▸ retry(2) ▸

    mapError()
  19. COMBINE DEBUGGING ▸ print() ▸ breakpointOnError() ▸ breakPoint()

  20. DEMO…

  21. Q&A…

  22. COMBINE RESOURCES ▸ Demo Project: https://github.com/ankush-bhatia/ SwiftIndia-Combine-Demo ▸ References: https://developer.apple.com/documentation/

    combine
  23. THANK YOU.