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.1k
Android 14 新機能 / Android 14 Meetup Nagoya
star_zero
1
550
Android 14 と Predictive back gesture / Shibuya.apk #42
star_zero
0
340
Coroutines Test 入門 / Android Test Night #8
star_zero
2
920
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
530
これからはじめるAndroid開発 / DevFest 2020
star_zero
4
680
Other Decks in Programming
See All in Programming
[PHPカンファレンス沖縄2024]「無理なくできるだけ安全に」テストもないレガシーコードをリファクタリングするテクニック
ikezoemakoto
3
130
pytest プラグインを開発して DRY に自動テストを書こう
inuatsu
2
260
dbt-ga4パッケージを実業務に導入してみた話
t_tokumaru_feedcorp
0
130
Removing Corepack
yosuke_furukawa
PRO
9
1.2k
タイミーにおけるデータの利用シーンと データ基盤の挑戦
marufeuille
4
3.2k
デバッグの話 / Debugging for Beginners
kaityo256
PRO
7
500
CSC509 Lecture 03
javiergs
PRO
0
140
ACES Meet におけるリリース作業改善の取り組み
fukucheee
0
130
Memory API: Patterns, Use Cases, and Performance
josepaumard
1
160
(Deep|Web) Link support with expo-router
mrtry
0
170
Micro Frontends for Java Microservices - dev2next 2024
mraible
PRO
0
210
What is TDD?
urakawa_jinsei
1
220
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Designing Experiences People Love
moore
138
23k
Typedesign – Prime Four
hannesfritz
39
2.3k
What's new in Ruby 2.0
geeforr
341
31k
The Language of Interfaces
destraynor
154
24k
Agile that works and the tools we love
rasmusluckow
327
21k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
39
2.1k
Into the Great Unknown - MozCon
thekraken
31
1.4k
Optimising Largest Contentful Paint
csswizardry
31
2.8k
Docker and Python
trallard
40
3k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
110
6.9k
Unsuck your backbone
ammeep
668
57k
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みたいなやつ
おわり