Slide 1

Slide 1 text

Comparative Asynchronous Programming

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Agenda 1. Asynchronous programming is hard. 2. Swift already supports a few async methodologies. 3. Swift supports some other paradigms. 4. But lacks support for others.

Slide 4

Slide 4 text

Asynchronous Programming: Difficult and Subjective

Slide 5

Slide 5 text

“Normal” programming

Slide 6

Slide 6 text

let returnValue = someFunctionCall()

Slide 7

Slide 7 text

Asynchronous programming

Slide 8

Slide 8 text

Development is all about tradeoffs

Slide 9

Slide 9 text

let file = readFile() ... readFile() { file in ... }

Slide 10

Slide 10 text

Embrace Tradeoffs

Slide 11

Slide 11 text

Swift Has Async Built-in. Sorta.

Slide 12

Slide 12 text

Built-in Async Approaches

Slide 13

Slide 13 text

Grand Central Dispatch

Slide 14

Slide 14 text

NSOperationQueue / DRBOperationTree

Slide 15

Slide 15 text

POSIX Threads

Slide 16

Slide 16 text

Target / Action

Slide 17

Slide 17 text

Callbacks / Completion Handlers

Slide 18

Slide 18 text

logIn(with: credentials) { result in // Handle login success or failure }

Slide 19

Slide 19 text

Callback Hell

Slide 20

Slide 20 text

getCredentialsFromUser() { credentials in logIn(with: credentials) { result in // Handle login success or failure } }

Slide 21

Slide 21 text

getCredentialsFromUser() { credentials, error in if credentials { logIn(with: credentials) { login, error in if login { // Handle login success } else { // Handle error } } } else { // Handle error } }

Slide 22

Slide 22 text

(credentials: Credentials?) -> Void (credentials: Credentials?, error: Error?) -> Void (result: Result) -> Void

Slide 23

Slide 23 text

enum Result { case success(T) case error(Error) }

Slide 24

Slide 24 text

Callback Heaven

Slide 25

Slide 25 text

Some Async Abstractions are Supported in Swift

Slide 26

Slide 26 text

Promises & Futures

Slide 27

Slide 27 text

getCredentialsFromUser() .flatMap { credentials in return login(with: credentials) } .onSuccess { login in // Handle login success } .onFailure { error in // Handle login error }

Slide 28

Slide 28 text

Functional Reactive Programming

Slide 29

Slide 29 text

getCredentialsFromUser() .flatMap { credentials in return login(with: credentials) } .on(next: { login in // Handle login success }, error: { error in // Handle login error })

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

gerstureRecognizer.rx .event .map { recognizer in return recognizer.location(in: recognizer.view) } .bind(to: circle.rx.center)

Slide 32

Slide 32 text

Actor Model

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Actor Model

Slide 35

Slide 35 text

Some Async Abstractions are Impossible in Swift

Slide 36

Slide 36 text

Async / Await

Slide 37

Slide 37 text

async func logIn() -> Login { let credentials = await getCredentialsFromUser() return await logIn(with: credentials) }

Slide 38

Slide 38 text

Coroutines and Generator Functions

Slide 39

Slide 39 text

func fibGenerator*() -> Int { var i = 0, j = 1 repeat { let next = i + j yield next (i, j) = (j, next) } while true } fibGenerator() // returns 1 fibGenerator() // returns 2 fibGenerator() // returns 3 fibGenerator() // returns 5

Slide 40

Slide 40 text

Wrap Up 1. There’s no “right way” to do async programming. 2. Asynchronous abstractions built into Swift aren’t great. 3. There are some cool things you can do in Swift. 4. There are other cool things that you can’t do in Swift.

Slide 41

Slide 41 text

No content