Slide 1

Slide 1 text

COMBINE DIVING INTO APPLE’S REACTIVE PROGRAMMING

Slide 2

Slide 2 text

ANKUSH BHATIA Sr. iOS Engineer @Mutual Mobile Twitter: @ankush1419 Github: ankush-bhatia Medium: @ankush_bhatia Linkedin: ankush-bhatia-ios Web: ankushbhatia.com

Slide 3

Slide 3 text

COMBINE INTRO

Slide 4

Slide 4 text

COMBINE INTRO

Slide 5

Slide 5 text

COMBINE EXISTING ASYNCHRONOUS APIS ▸ Notification Center ▸ Delegates ▸ GCD / Operation Queues ▸ Closures

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

COMBINE PILLARS OF COMBINE COMBINE PUBLISHER OPERATOR SUBSCRIBER

Slide 9

Slide 9 text

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.)

Slide 10

Slide 10 text

COMBINE PUBLISHER PROTOCOL

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

COMBINE SUBSCRIBER PROTOCOL

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

COMBINE TYPE OF OPERATORS Source: WWDC Slides

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

COMBINE IS IT REALLY JUST AN ALTERNATE APPROACH?

Slide 17

Slide 17 text

COMBINE MORE TO COMBINE ▸ Memory Management ▸ Cancellable Protocol ▸ [AnyCancellable] ▸ Testability

Slide 18

Slide 18 text

COMBINE ERROR HANDLING ▸ assertNoFailure() ▸ catch() ▸ retry(2) ▸ mapError()

Slide 19

Slide 19 text

COMBINE DEBUGGING ▸ print() ▸ breakpointOnError() ▸ breakPoint()

Slide 20

Slide 20 text

DEMO…

Slide 21

Slide 21 text

Q&A…

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

THANK YOU.