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
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
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
20210625-meet-async-await@swift愛好会
Search
iganin
June 25, 2021
Technology
1.8k
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
Meet AgentCore Identity Consent Portal
hironobuiga
3
230
On-behalf-of Token exchange with AgentCore Identity
hironobuiga
2
340
FastMCP OAuth Proxy with Cognito
hironobuiga
4
460
Three-Legged OAuth in AgentCore Gateway
hironobuiga
2
360
Amazon Bedrock AgentCore 認証・認可入門
hironobuiga
3
690
AgentCoreで実現するマルチテナントAIエージェント
hironobuiga
2
300
FlutterでGraphQL のuseQueryを使う
hironobuiga
0
43
FlutterでGraphQLを扱う
hironobuiga
0
32
FlutterでScrollViewとExpandedを併用し てSignIn / SignUp画面 など レイアウトを作成する
hironobuiga
0
33
Other Decks in Technology
See All in Technology
不可逆な意思決定を避けながら10億超の顧客データを AI Ready にするデータ基盤設計
baseballyama
1
300
Kiro Crewをはじめよう
kizawa2020
1
140
実体験から学ぶ分析エージェントの開発と運⽤
recruitengineers
PRO
8
2.1k
全PRの83%が AIレビューだけでマージできるようになった開発組織は その後どうなったか @2026/10
athug
0
140
ビジネスを止めない技術的負債の返済のための戦略とその手法 - 技術的負債と向き合う / Complexity and Simplicity
soudai
PRO
3
690
夫婦で子育てするための育児標準化
codmoninc
PRO
0
160
並行性の問題を防げ!実践トランザクション入門
occhi
0
280
「Docker Desktop不要の時代」は 来たのか?
ueponx
0
510
使いこなすために知っておきたい Azure SRE Agent アンチパターン
torumakabe
2
480
ゲームのリアルタイム通信から見た QUIC と MoQ
segadevtech
0
110
SDDの運用にめげずに向き合った話
sansantech
PRO
1
140
Loop Engineering これくらいでいい
mikanichinose
0
220
Featured
See All Featured
The SEO identity crisis: Don't let AI make you average
varn
0
580
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
sira's awesome portfolio website redesign presentation
elsirapls
0
450
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
930
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
How to train your dragon (web standard)
notwaldorf
96
6.9k
The SEO Collaboration Effect
kristinabergwall1
1
590
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
710
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
67k
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以上でないと使用できないようです