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. 自己紹介 @sobaya15 : Twitter @sobaya-0141 :Github @sobaya : Qiita Android

    iOS 今すぐインストール! いろんな人を募集してます!! お気軽にお声がけください
  2. 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")
  3. 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
  4. 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
  5. 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")
  6. Channel vs Flow Channel print() getList() Channel A B C

    runBlocking { val channel = getChannelList() for (result in channel) { Log.d("CHANNEL", "PRINT $result") } } ファイル読み込みとか通信 だったらcloseされない
  7. 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しなきゃ動か ないから安心
  8. List vs Flow List print() getList() A B C A

    B C Flow print() getList() A B C A C B
  9. 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