Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
20210625-meet-async-await@swift愛好会
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
iganin
June 25, 2021
Technology
1.7k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
20210625-meet-async-await@swift愛好会
iganin
June 25, 2021
More Decks by iganin
See All by iganin
On-behalf-of Token exchange with AgentCore Identity
hironobuiga
2
320
FastMCP OAuth Proxy with Cognito
hironobuiga
4
390
Three-Legged OAuth in AgentCore Gateway
hironobuiga
2
340
Amazon Bedrock AgentCore 認証・認可入門
hironobuiga
3
680
AgentCoreで実現するマルチテナントAIエージェント
hironobuiga
2
220
FlutterでGraphQL のuseQueryを使う
hironobuiga
0
36
FlutterでGraphQLを扱う
hironobuiga
0
30
FlutterでScrollViewとExpandedを併用し てSignIn / SignUp画面 など レイアウトを作成する
hironobuiga
0
32
Data Race and Actor
hironobuiga
0
100
Other Decks in Technology
See All in Technology
AIとペアプロを始める。人とのペアプロをやめる。ペアプロの良さを改めて知る。もっと好きになった。 / Rediscovering Pair Programming
honyanya
1
310
プロダクトエンジニアに必要な「いい感じ」に作る能力 〜たくさん作れる時代に、どこまで作るかの決め方〜
jnishime_dresscode
2
1.4k
『止めない』を設計する — 制約の中で、事業の根幹を支える判断
hiroyaterui
0
240
From Vanilla Kubernetes to a Batteries-Included Platform: Developer Experience at 1,300+ Clusters
yosshi_
0
650
2026/09/10 Spring_Bootから_Jakarta_EE_MicroProfileへの移行
megascus
0
260
Omarchy Quattro の日本語設定周り
simosako
2
100
全員がプロダクトへ向き合う組織を持続成長させるために——組織づくりのフライホイールと4象限 / The Flywheel Model and Four Quadrants for Organizational Design
hiro_torii
4
930
module Synths; end
asonas
1
130
When Does a Local Qwen Start to Break
morshoto
0
190
20260912_スクフェス三河
kgnkhkr
0
100
AI-DLCって実際どう? 〜聞きたいこと全部聞いてみる〜
news_it_enj
0
250
こんなアーキテクチャ図は嫌だ BEYOND THE TIME: 半年後の自分へ贈る15のメッセージ / 15 of Anti-pattern in AWS Architecture Diagrams
naospon
3
280
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
970
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
410
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
480
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Raft: Consensus for Rubyists
vanstee
141
7.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
990
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
370
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
Mind Mapping
helmedeiros
1
350
Building Adaptive Systems
keathley
44
3.2k
Transcript
Meet async/await in Swift 集まれSwift好き!Swift愛好会スピンオフ WWDC21セッション要約会 @オンライン 2021/06/25 Hironobu Iga
自己紹介 • Hironobu Iga • Flutter, React, Ktor(Kotlin), iOS •
Twitter - @iganin_dev
同期処理と非同期処理
非同期処理 非同期処理を活用することでスレッドを有効に使える(通信、DB etc)
非同期処理 非同期処理を活用することでスレッドを有効に使える(通信、DB etc) 同期処理はスレッドを ブロックする
非同期処理 非同期処理を活用することでスレッドを有効に使える(通信、DB etc) 非同期処理によって処理の 待ち時間に他の処理を差し 込むことができる
非同期処理の例
None
サムネイル画像を取得する
同期処理
非同期処理
従来のCompletion Handler を使用した方法
None
ネストが深い
エラーハンドリングを completionで伝える必要がある 漏れていてもコンパイラが検知してくれない 同様に値の返却も忘れる可能性があり、こちらもコンパイ ラが検知してくれない
実行順が複雑で処理を追うのが難しい
async / await を使った方法
None
内部で中断する処理が入る場合は asyncをつける 例外を投げる場合は、 async throwsの順 でないとコンパイルエラー ※SE-0296に詳細
中断する処理には await をつける 例外を投げる可能性がある場合は try await ※ await try はコンパイルエラー
例外が発生しうる処理は try await と記載され、 例外処理の実装漏れの可能性がなくなった completionの書き漏れによる値の返却漏れもなく なった
実行順が上から下になり、 処理が格段に追いやすくなった
getter ( setter は不可 ) - SE-0310, for 文 init(
deinit は不可 ), subscript ( arrray[i]のようなアクセス) async / await が使用できるケース
上記は await の関数を直列に順次実行していく 同時に並列に実行する方法は、 Structured concurrency in Swift セッション で確認可能
async / await になったメソッドをSwiftUIで使う
async / await になったメソッドをSwiftUIで使う
async / await になったメソッドをSwiftUIで使う 中断をする関数をawait付きで呼び出 すために async { } で囲う必要がある
• URLSession • MapKit • HealthKit • NSDocument • etc...
asnyc / await はさまざまなSDKに導入済み
• URLSession • MapKit • HealthKit • NSDocument • etc...
asnyc / await はさまざまなSDKに導入済み
Continuation
Continuation • 中断している処理を保持し、処理の再開(resume)を明示的に行える ◦ 実装を見ているとCompletion Handlerに似てる気がする • 再開(resume)は一度しか呼べない • withCheckedContinuation
◦ 呼び出しが一度だけかどうか、呼び出し忘れていないかどうかを実行時にチェックしてくれる ◦ 2度呼び出した場合はクラッシュ (CONTINUATION MISUSE) • withUnsafeContinuation ◦ 呼び出しが一度だけか、呼び出し忘れがないかのチェックがない ◦ 2度呼び出した場合はクラッシュ (EXC_BAD_ACCESS) ◦ SDKなどでのパフォーマンス効率化以外では極力使用しない
Continuationを使う例 以下の処理を考える • PeerManagerの syncSharedPostメソッドを呼び出す • 返却値かエラーをDelegateメソッドを介して受け取る 上記ををasync / await
を使用した形に書き換える
メソッド呼び出しの結果を delegateメソッド経 由で受け取る
メソッド呼び出しに関する Continuationをクラ スのプロパティに保持する ( CompletionHandlerを保持するのと似てる )
Delegateメソッドに値が返ってきたタイミング で結果とともにcontinuationのrsumeを呼 ぶ continuationは1度しかresumeできないた め保持していたcontinuationは破棄する
まとめ • async / await で非同期処理がシンプルにかける • getter, for文 initializer,
subscript etcで使える • completionHandler の async / await への置き換え • Delegationメソッドのasync / awaitへの置き換え • 複数のSDKにすでに導入済み • Continuationについて • セッションにはなかったですが、 iOS15以上でないと使用できないようです