Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Comparative Asynchronous Programming
Ash Furrow
February 24, 2017
Programming
2
8.5k
Comparative Asynchronous Programming
Presented at Playgrounds Conf:
http://www.playgroundscon.com/
Ash Furrow
February 24, 2017
Tweet
Share
More Decks by Ash Furrow
See All by Ash Furrow
Migrating to React Native: A Long-Term Retrospective
ashfurrow
0
130
How Artsy Automates Team Culture
ashfurrow
0
2.1k
Building Custom TSLint Rules
ashfurrow
0
220
Circumventing Fear of the Unknown
ashfurrow
1
290
Building Better Software by Building Better Teams
ashfurrow
1
350
Building Open Source Communities
ashfurrow
0
460
Building Compassionate Software
ashfurrow
0
220
Swift, Briskly
ashfurrow
0
87
iOS Checkup
ashfurrow
1
690
Other Decks in Programming
See All in Programming
42tokyo-born2beroot-review
love42
0
110
Showkase、Paparazziを用いたビジュアルリグレッションテストの導入にチャレンジした話 / MoT TechTalk #15
mot_techtalk
0
120
(新米)エンジニアリングマネージャーのしごと #RSGT2023
murabayashi
9
5.8k
LIFFで動く割り勘アプリTATEKAをリリースしてみた話
inoue2002
0
260
Azure Functionsをサクッと開発、サクッとデプロイ/vscodeconf2023-baba
nina01
1
350
量子コンピュータ時代のプログラミングセミナー / 20230119_Amplify_seminar _shift_optimization
fixstars
0
190
Makuakeの認証基盤とRe-Architectureチーム
bmf_san
0
610
コンピュータビジョンセミナー2 / computer_vision_seminar_libSGM
fixstars
0
320
ECテックカンファレンス2023
kspace
1
360
僕が考えた超最強のKMMアプリの作り方
spbaya0141
0
180
2023年にクル(かもしれない)通信ミドルウェア技術(仮)
s_hosoai
0
220
フロントエンドで学んだことをデータ分析で使ってみた話
daichi_igarashi
0
190
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
240
11k
Producing Creativity
orderedlist
PRO
335
38k
Creatively Recalculating Your Daily Design Routine
revolveconf
207
11k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
44
14k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
31
20k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
236
1.1M
Bash Introduction
62gerente
601
210k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
32
6.7k
Writing Fast Ruby
sferik
613
58k
Side Projects
sachag
451
37k
Making the Leap to Tech Lead
cromwellryan
117
7.7k
Statistics for Hackers
jakevdp
785
210k
Transcript
Comparative Asynchronous Programming
None
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.
Asynchronous Programming: Difficult and Subjective
“Normal” programming
let returnValue = someFunctionCall()
Asynchronous programming
Development is all about tradeoffs
let file = readFile() ... readFile() { file in ...
}
Embrace Tradeoffs
Swift Has Async Built-in. Sorta.
Built-in Async Approaches
Grand Central Dispatch
NSOperationQueue / DRBOperationTree
POSIX Threads
Target / Action
Callbacks / Completion Handlers
logIn(with: credentials) { result in // Handle login success or
failure }
Callback Hell
getCredentialsFromUser() { credentials in logIn(with: credentials) { result in //
Handle login success or failure } }
getCredentialsFromUser() { credentials, error in if credentials { logIn(with: credentials)
{ login, error in if login { // Handle login success } else { // Handle error } } } else { // Handle error } }
(credentials: Credentials?) -> Void (credentials: Credentials?, error: Error?) -> Void
(result: Result<Credentials>) -> Void
enum Result<T> { case success(T) case error(Error) }
Callback Heaven
Some Async Abstractions are Supported in Swift
Promises & Futures
getCredentialsFromUser() .flatMap { credentials in return login(with: credentials) } .onSuccess
{ login in // Handle login success } .onFailure { error in // Handle login error }
Functional Reactive Programming
getCredentialsFromUser() .flatMap { credentials in return login(with: credentials) } .on(next:
{ login in // Handle login success }, error: { error in // Handle login error })
None
gerstureRecognizer.rx .event .map { recognizer in return recognizer.location(in: recognizer.view) }
.bind(to: circle.rx.center)
Actor Model
None
Actor Model
Some Async Abstractions are Impossible in Swift
Async / Await
async func logIn() -> Login { let credentials = await
getCredentialsFromUser() return await logIn(with: credentials) }
Coroutines and Generator Functions
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
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.
None