Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynxronous Datastreaming with Kotlin Flow

2f2c6995048700e9175943d868df4974?s=47 sobaya
January 17, 2020

Asynxronous Datastreaming with Kotlin Flow

kotlin conf

2f2c6995048700e9175943d868df4974?s=128

sobaya

January 17, 2020
Tweet

More Decks by sobaya

Other Decks in Programming

Transcript

  1. Asynchronous Data Streams with Kotlin Flow を要約したかった

  2. 自己紹介 @sobaya15 : Twitter @sobaya-0141 :Github @sobaya : Qiita Android

    iOS 今すぐインストール! いろんな人を募集してます!! お気軽にお声がけください
  3. やることリスト ・List vs Channel ・Channel vs Flow ・List vs Flow

    ・元ネタや参考情報
  4. List vs Channel

  5. List vs Channel runBlocking { val list = getList() list.forEach

    { Log.d("LIST", "PRINT $it") } } runBlocking { val channel = getChannelList() for (result in channel) { Log.d("CHANNEL", "PRINT $result") } } List Channel Log.d("LIST", "BEFORE ADD A") list.add("A") Log.d("LIST", "AFTER ADD A") Log.d("CHANNEL", "BEFORE SEND A") send("A") Log.d("CHANNEL", "AFTER SEND A")
  6. List vs Channel 2020-01-09 12:04:57.588 15021-15021/com.example.myapplication D/LIST: BEFORE ADD A

    2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: AFTER ADD A 2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: BEFORE ADD B 2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: AFTER ADD B 2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: BEFORE ADD C 2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: AFTER ADD C 2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: PRINT A 2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: PRINT B 2020-01-09 12:04:57.589 15021-15021/com.example.myapplication D/LIST: PRINT C List print() getList() A B C A B C
  7. List vs Channel 2020-01-09 12:06:44.706 15021-15021/com.example.myapplication D/CHANNEL: BEFORE SEND A

    2020-01-09 12:06:44.706 15021-15021/com.example.myapplication D/CHANNEL: AFTER SEND A 2020-01-09 12:06:44.706 15021-15021/com.example.myapplication D/CHANNEL: BEFORE SEND B 2020-01-09 12:06:44.707 15021-15021/com.example.myapplication D/CHANNEL: PRINT A 2020-01-09 12:06:44.708 15021-15021/com.example.myapplication D/CHANNEL: PRINT B 2020-01-09 12:06:44.708 15021-15021/com.example.myapplication D/CHANNEL: AFTER SEND B 2020-01-09 12:06:44.708 15021-15021/com.example.myapplication D/CHANNEL: BEFORE SEND C 2020-01-09 12:06:44.708 15021-15021/com.example.myapplication D/CHANNEL: AFTER SEND C 2020-01-09 12:06:44.710 15021-15021/com.example.myapplication D/CHANNEL: PRINT C Channel print() getList() A B C Channel A B C
  8. Channel vs Flow

  9. Channel vs Flow val flow = getFlowList() flow.collect { result

    -> Log.d("FLOW", "PRINT $result") } Flow Log.d("FLOW", "BEFORE EMIT A") emit("A") Log.d("FLOW", "AFTER EMIT A") runBlocking { val channel = getChannelList() for (result in channel) { Log.d("CHANNEL", "PRINT $result") } } Channel Log.d("CHANNEL", "BEFORE SEND A") send("A") Log.d("CHANNEL", "AFTER SEND A")
  10. Channel vs Flow Channel print() getList() Channel A B C

    runBlocking { val channel = getChannelList() for (result in channel) { Log.d("CHANNEL", "PRINT $result") } } ファイル読み込みとか通信 だったらcloseされない
  11. Channel vs Flow Flow print() getList() Flow A B C

    val flow = getFlowList() flow.collect { result -> Log.d("FLOW", "PRINT $result") } collect A B C collectしなきゃ動か ないから安心
  12. List vs Flow

  13. List vs Flow List print() getList() A B C A

    B C Flow print() getList() A B C A C B
  14. List vs Flow 処理が交互に行われる ようになっただけじゃ ね???

  15. List vs Flow bufferを使うがよい

  16. List vs Flow Flow val flow = getFlowList() flow.buffer().collect {

    result -> Log.d("FLOW", "PRINT $result") }
  17. List vs Flow print() getList() Flow collect 2020-01-10 12:50:16.181 11661-11661/com.example.myapplication

    D/FLOW: BEFORE EMIT A 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: AFTER EMIT A 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: BEFORE EMIT B 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: AFTER EMIT B 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: BEFORE EMIT C 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: AFTER EMIT C 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: PRINT A 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: PRINT B 2020-01-10 12:50:16.182 11661-11661/com.example.myapplication D/FLOW: PRINT C A B C A B C
  18. List vs Flow ・紹介したように引数なしでbufferを呼んだ場合は64個 が上限でバッファしてくれる ・バッファサイズを任意に指定可能 ・UNLIMITEDを指定すると無制限(Int.MAX_VLUE) バッファサイズ以外に動作も指定できるので、単純に順 番通りの値が欲しい場合以外には調べてもらえるとい いかもです。

  19. 元ネタや参考情報 公式 https://www.youtube.com/watch?v=tYcqn48SMT8&t=1401s https://kotlinlang.org/docs/reference/coroutines/flow.html YouTube https://satoshun.github.io/2019/08/flow-buffer/ bufferに関するブログ 発表に使ったサンプルコード https://github.com/sobaya-0141/CoroutineSample