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
330
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
4
4k
Android 14 新機能 / Android 14 Meetup Nagoya
star_zero
1
500
Android 14 と Predictive back gesture / Shibuya.apk #42
star_zero
0
300
Coroutines Test 入門 / Android Test Night #8
star_zero
2
740
What's new in Jetpack / I/O Extended Japan 2022
star_zero
1
580
Kotlin 2021 Recap / DevFest 2021
star_zero
3
1.2k
Kotlin Symbol Processing (KSP) を使ったコード生成 / DroidKaigi 2021
star_zero
2
5k
What's new Android 12
star_zero
0
520
これからはじめるAndroid開発 / DevFest 2020
star_zero
4
670
Other Decks in Programming
See All in Programming
Milestoner
bkuhlmann
1
420
AppRouter Panel Talk
yosuke_furukawa
PRO
1
500
Goのmultiple errorsについて (2024年4月版)
syumai
4
1.3k
SIMD Parallel Programming with the Vector API
josepaumard
0
240
WebGLで始める コンピュータグラフィックス入門
heller77
0
350
Hanami and htmx
bkuhlmann
0
230
CDKコントリビュートの最初の壁を越えよう! -簡単issueの見つけ方-
badmintoncryer
3
350
Open standards for building event-driven applications in the cloud
meteatamel
0
190
Revisiting the Hotwire Landscape after Turbo 8 @ RailsConf 2024, Detroit
marcoroth
0
210
Snowflakeで眠ったデータを起こそう!
estie
0
160
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
290
Implementing Design Systems in Swift
seyfoyun
2
500
Featured
See All Featured
Clear Off the Table
cherdarchuk
85
310k
A designer walks into a library…
pauljervisheath
201
23k
Being A Developer After 40
akosma
67
580k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
21
1.6k
BBQ
matthewcrist
80
8.8k
GraphQLの誤解/rethinking-graphql
sonatard
56
9.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
Designing with Data
zakiwarfel
96
4.8k
Why Our Code Smells
bkeepers
PRO
331
56k
Building Flexible Design Systems
yeseniaperezcruz
320
37k
Building a Modern Day E-commerce SEO Strategy
aleyda
22
6.4k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
15
1.6k
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みたいなやつ
おわり