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
350
Coroutines Flow 入門 / Coroutines Flow Introduction
star_zero
August 05, 2019
Tweet
Share
More Decks by star_zero
See All by star_zero
今からはじめるAndroidアプリ開発 2024 / DevFest 2024
star_zero
0
1k
Jetpack Compose の Side-effect を使いこなす / DroidKaigi 2023
star_zero
5
5.5k
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
1k
What's new in Jetpack / I/O Extended Japan 2022
star_zero
1
620
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
550
Other Decks in Programming
See All in Programming
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
440
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
790
良いユニットテストを書こう
mototakatsu
8
2.5k
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
Recoilを剥がしている話
kirik
5
6.8k
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
390
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
800
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
800
Featured
See All Featured
Gamification - CAS2011
davidbonilla
80
5.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Speed Design
sergeychernyshev
25
670
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
The Invisible Side of Design
smashingmag
298
50k
Faster Mobile Websites
deanohume
305
30k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Building Your Own Lightsaber
phodgson
103
6.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
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みたいなやつ
おわり