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
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
670
layerx_20241129.pdf
kyoheig3
2
290
Refactor your code - refactor yourself
xosofox
1
260
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
530
Security_for_introducing_eBPF
kentatada
0
110
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
137
6.7k
Making Projects Easy
brettharned
116
5.9k
Music & Morning Musume
bryan
46
6.2k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Optimizing for Happiness
mojombo
376
70k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Side Projects
sachag
452
42k
Writing Fast Ruby
sferik
628
61k
Become a Pro
speakerdeck
PRO
26
5k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
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みたいなやつ
おわり