Coroutines Flow 入門 / Coroutines Flow Introduction

80a3a3857a55f154d23acb705eff72cc?s=47 star_zero
August 05, 2019

Coroutines Flow 入門 / Coroutines Flow Introduction

80a3a3857a55f154d23acb705eff72cc?s=128

star_zero

August 05, 2019
Tweet

Transcript

  1. Coroutines Flow 入門 Kenji Abe

  2. Coroutines Flow とは •雑に言うと、RxJavaみたいなやつ •Cold Stream ‣ 受信しはじめてから動き始める ‣ Hot

    Stream は Channel
  3. Coroutines Flow とは • ↓ の資料が分かりやすいので、そっちを見て ‣ https://speakerdeck.com/sys1yagi/5fen- tewakarukotlin-coroutines-flow •この資料では色々な使い方を紹介します

  4. 基本的なやつ fun main() = runBlocking { val flow = flow

    { repeat(3) { emit(it) } } flow.collect { println(it) } }
  5. 基本的なやつ fun main() = runBlocking { val flow = flow

    { repeat(3) { emit(it) } } flow.collect { println(it) } } Flow builder
  6. 基本的なやつ fun main() = runBlocking { val flow = flow

    { repeat(3) { emit(it) } } flow.collect { println(it) } } 値を送出
  7. 基本的なやつ fun main() = runBlocking { val flow = flow

    { repeat(3) { emit(it) } } flow.collect { println(it) } } 値を受信
  8. Flow builders •Flow { } •flowOf(...) ‣ flowOf(1, 2, 3)

    •(() -> T).asFlow() ‣ ({ 1 }).asFlow()
  9. Intermediate operators •map •filter •take •zip

  10. Flow operators fun main() = runBlocking { val flow =

    flowOf(1, 2, 3, 4, 5) flow.map { it * 2 }.collect { println(it) } } 値を2倍
  11. Flow operators fun main() = runBlocking { val flow =

    flowOf(1, 2, 3, 4, 5) flow.filter { it % 2 == 0 }.collect { println(it) } } 偶数のみ
  12. Terminal operators •collect •single •reduce •toList

  13. Terminal operators fun main() = runBlocking { val flow =

    flowOf(1, 2, 3, 4, 5) val value = flow .filter { it == 5 } .single() println(value) // 5 } 複数の値の場合は例外になる
  14. 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 }
  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] }
  16. Change context •flowOn ‣ 上流のContextを変更する ‣ Flowにおいて唯一Contextを切り替える方法 •emitするときにContextが異なると例外

  17. 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
  18. 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 { } }
  19. Change context fun main() = runBlocking { val flow =

    flowOf(1, 2, 3, 4, 5) flow .map { } .flowOn(Dispatchers.IO) .flowOn(Dispatchers.Default) .collect { } } 最初のやつが優先
  20. Change context fun main() = runBlocking { val flow =

    flow { emit(1) launch { emit(2) } } flow.collect { } } emit時にContextが違うので 例外になる
  21. Exception •catchを使って捕捉できる •上流のストリームの例外のみ捕捉 •onCompletionでfinallyみたいなこともできる

  22. Exception fun main() = runBlocking { flow { } .map

    { } .catch { } .map { } .catch { } .collect { } } ① ② ③ ③の例外はここで捕捉 ①と②の例外はここで捕捉 それ以降は実行されない
  23. Exception fun main() = runBlocking { flow { emit(1) }.catch

    { emit(-1) }.collect { println(it) } } 例外発生時に別の値を送出
  24. Exception fun main() = runBlocking { flow { } .map

    { } .onCompletion { } .map { } .onCompletion { } .collect { } } finallyみたいなやつ
  25. おわり