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
340
On-behalf-of Token exchange with AgentCore Identity
hironobuiga
2
400
FastMCP OAuth Proxy with Cognito
hironobuiga
4
470
Three-Legged OAuth in AgentCore Gateway
hironobuiga
2
400
Amazon Bedrock AgentCore 認証・認可入門
hironobuiga
3
700
AgentCoreで実現するマルチテナントAIエージェント
hironobuiga
2
370
FlutterでGraphQL のuseQueryを使う
hironobuiga
0
50
FlutterでGraphQLを扱う
hironobuiga
0
42
FlutterでScrollViewとExpandedを併用し てSignIn / SignUp画面 など レイアウトを作成する
hironobuiga
0
40
Other Decks in Technology
See All in Technology
現場を動かすAIエージェント#2_製造業現場でのAI活用から見た、ハマる罠と実践知
kintotechdev
1
120
Devin プロモコードの受け取りと適用(2026.10.27)
kinopeee
1
840
基本から分かる!AWSセキュリティ運用の考え方と任せ方
cmusudakeisuke
1
380
Maestro MCPとClaude Code Skillsによる自律的なE2Eテスト支援
asmz
0
410
AIがGISを代替する日:〜すべてをAIが創る時代に、私たちがFOSS4Gに関わる意味〜
lehupa
0
200
20261031_IoTLT_vol140_kitazaki_v1.pdf
kitazaki
0
160
コードレビューをなくして見えたもの
henteko
0
130
Deep Data Security 機能解説
oracle4engineer
PRO
3
1k
続・ソフトウェアアーキテクトのための意思決定術: Decision Readiness, Revisited — When AI Changes the Economics of Software Architecture
snoozer05
PRO
4
980
WireGuard for Apache NuttX - Bringing Secure Remote Access to embedded Devices -
aki310ooooo
0
140
Don't write the architecture only in the prompt.
neohsu
0
300
慶應義塾大学 AI実践05 論文執筆の仕組み化
keio_smilab
PRO
6
3.5k
Featured
See All Featured
Facilitating Awesome Meetings
lara
57
7.2k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
320
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
870
What does AI have to do with Human Rights?
axbom
PRO
1
2.5k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
620
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
300
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
390
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Rails Girls Zürich Keynote
gr2m
96
14k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.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以上でないと使用できないようです