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

Asynxronous Datastreaming with Kotlin Flow

sobaya
January 17, 2020

Asynxronous Datastreaming with Kotlin Flow

kotlin conf

sobaya

January 17, 2020
Tweet

More Decks by sobaya

Other Decks in Programming

Transcript

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

    View full-size slide

  2. 自己紹介
    @sobaya15 : Twitter
    @sobaya-0141 :Github
    @sobaya : Qiita
    Android iOS
    今すぐインストール!
    いろんな人を募集してます!!
    お気軽にお声がけください

    View full-size slide

  3. やることリスト
    ・List vs Channel
    ・Channel vs Flow
    ・List vs Flow
    ・元ネタや参考情報

    View full-size slide

  4. List vs Channel

    View full-size slide

  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")

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  8. Channel vs Flow

    View full-size slide

  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")

    View full-size slide

  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されない

    View full-size slide

  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しなきゃ動か
    ないから安心

    View full-size slide

  12. List vs Flow

    View full-size slide

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

    View full-size slide

  14. List vs Flow
    処理が交互に行われる
    ようになっただけじゃ
    ね???

    View full-size slide

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

    View full-size slide

  16. List vs Flow
    Flow
    val flow = getFlowList()
    flow.buffer().collect { result ->
    Log.d("FLOW", "PRINT $result")
    }

    View full-size slide

  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

    View full-size slide

  18. List vs Flow
    ・紹介したように引数なしでbufferを呼んだ場合は64個
    が上限でバッファしてくれる
    ・バッファサイズを任意に指定可能
    ・UNLIMITEDを指定すると無制限(Int.MAX_VLUE)
    バッファサイズ以外に動作も指定できるので、単純に順
    番通りの値が欲しい場合以外には調べてもらえるとい
    いかもです。

    View full-size slide

  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

    View full-size slide