Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Coroutines Flow 入門 / Coroutines Flow Introduction
Search
star_zero
August 05, 2019
Programming
0
340
Coroutines Flow 入門 / Coroutines Flow Introduction
star_zero
August 05, 2019
Tweet
Share
More Decks by star_zero
See All by star_zero
Jetpack Compose の Side-effect を使いこなす / DroidKaigi 2023
star_zero
5
5.3k
Android 14 新機能 / Android 14 Meetup Nagoya
star_zero
1
560
Android 14 と Predictive back gesture / Shibuya.apk #42
star_zero
0
350
Coroutines Test 入門 / Android Test Night #8
star_zero
2
970
What's new in Jetpack / I/O Extended Japan 2022
star_zero
1
610
Kotlin 2021 Recap / DevFest 2021
star_zero
3
1.2k
Kotlin Symbol Processing (KSP) を使ったコード生成 / DroidKaigi 2021
star_zero
2
5.1k
What's new Android 12
star_zero
0
540
これからはじめるAndroid開発 / DevFest 2020
star_zero
4
680
Other Decks in Programming
See All in Programming
Better Code Design in PHP
afilina
PRO
0
120
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
1
110
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
150
Outline View in SwiftUI
1024jp
1
320
受け取る人から提供する人になるということ
little_rubyist
0
230
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
310
Arm移行タイムアタック
qnighy
0
310
Jakarta EE meets AI
ivargrimstad
0
520
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
20
1.1k
The Pragmatic Product Professional
lauravandoore
31
6.3k
A designer walks into a library…
pauljervisheath
203
24k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Building Adaptive Systems
keathley
38
2.3k
A Philosophy of Restraint
colly
203
16k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Designing Experiences People Love
moore
138
23k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
GitHub's CSS Performance
jonrohan
1030
460k
Transcript
Coroutines Flow 入門 Kenji Abe
Coroutines Flow とは •雑に言うと、RxJavaみたいなやつ •Cold Stream ‣ 受信しはじめてから動き始める ‣ Hot
Stream は Channel
Coroutines Flow とは • ↓ の資料が分かりやすいので、そっちを見て ‣ https://speakerdeck.com/sys1yagi/5fen- tewakarukotlin-coroutines-flow •この資料では色々な使い方を紹介します
基本的なやつ fun main() = runBlocking { val flow = flow
{ repeat(3) { emit(it) } } flow.collect { println(it) } }
基本的なやつ fun main() = runBlocking { val flow = flow
{ repeat(3) { emit(it) } } flow.collect { println(it) } } Flow builder
基本的なやつ fun main() = runBlocking { val flow = flow
{ repeat(3) { emit(it) } } flow.collect { println(it) } } 値を送出
基本的なやつ fun main() = runBlocking { val flow = flow
{ repeat(3) { emit(it) } } flow.collect { println(it) } } 値を受信
Flow builders •Flow { } •flowOf(...) ‣ flowOf(1, 2, 3)
•(() -> T).asFlow() ‣ ({ 1 }).asFlow()
Intermediate operators •map •filter •take •zip
Flow operators fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) flow.map { it * 2 }.collect { println(it) } } 値を2倍
Flow operators fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) flow.filter { it % 2 == 0 }.collect { println(it) } } 偶数のみ
Terminal operators •collect •single •reduce •toList
Terminal operators fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) val value = flow .filter { it == 5 } .single() println(value) // 5 } 複数の値の場合は例外になる
Terminal operators fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) val value = flow .reduce { accumulator, value -> accumulator + value } // 1 + 2 + 3 + 4 + 5 println(value) // 15 }
Terminal operators fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) val value = flow .toList() println(value) // [1, 2, 3, 4, 5] }
Change context •flowOn ‣ 上流のContextを変更する ‣ Flowにおいて唯一Contextを切り替える方法 •emitするときにContextが異なると例外
Change context fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) flow .map { println("map: ${Thread.currentThread().name}") it + 1 } .flowOn(Dispatchers.IO) .collect { println("collect: ${Thread.currentThread().name}") } } worker main
Change context fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) flow .map { } .flowOn(Dispatchers.IO) .filter { } .flowOn(Dispatchers.Default) .map { } .flowOn(Dispatchers.IO) .collect { } }
Change context fun main() = runBlocking { val flow =
flowOf(1, 2, 3, 4, 5) flow .map { } .flowOn(Dispatchers.IO) .flowOn(Dispatchers.Default) .collect { } } 最初のやつが優先
Change context fun main() = runBlocking { val flow =
flow { emit(1) launch { emit(2) } } flow.collect { } } emit時にContextが違うので 例外になる
Exception •catchを使って捕捉できる •上流のストリームの例外のみ捕捉 •onCompletionでfinallyみたいなこともできる
Exception fun main() = runBlocking { flow { } .map
{ } .catch { } .map { } .catch { } .collect { } } ① ② ③ ③の例外はここで捕捉 ①と②の例外はここで捕捉 それ以降は実行されない
Exception fun main() = runBlocking { flow { emit(1) }.catch
{ emit(-1) }.collect { println(it) } } 例外発生時に別の値を送出
Exception fun main() = runBlocking { flow { } .map
{ } .onCompletion { } .map { } .onCompletion { } .collect { } } finallyみたいなやつ
おわり