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

Kotlin コルーチンを 理解しよう

yagi
August 25, 2018

Kotlin コルーチンを 理解しよう

- コルーチンとはなにか
- Kotlinはどのようにコルーチンを実現しているのか
- Kotlin コルーチンの基本的な使い方

yagi

August 25, 2018
Tweet

More Decks by yagi

Other Decks in Technology

Transcript

  1. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJO'FTU
    5PTIJIJSP:BHJ
    ,PUMJOίϧʔνϯΛ
    ཧղ͠Α͏

    View full-size slide

  2. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wീ໦ढ़޿
    w!TZTZBHJ
    w$50PG
    w"OESPJE&OHJOFFS
    "CPVU.F
    Jetpack Handbook ʲC94৽ץʳ
    ʮୈ6ষ WorkManagerͱόοΫάϥ΢ϯυλεΫʯ
    ॻ͖·ͨ͠

    View full-size slide

  3. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ©2018 Lang-8 Inc. ALL Rights Reserved.
    https://hinative.com
    8FBSFIJSJOH

    View full-size slide

  4. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wίϧʔνϯͱ͸ͳʹ͔
    w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ
    ͍ͯ͠Δͷ͔
    w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ
    ࠓ೔࿩͢͜ͱ

    View full-size slide

  5. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wίϧʔνϯͱ͸ͳʹ͔
    w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ
    ͍ͯ͠Δͷ͔
    w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ
    ࠓ೔࿩͢͜ͱ

    View full-size slide

  6. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wίϧʔνϯͱ͸ͳʹ͔
    w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ
    ͍ͯ͠Δͷ͔
    w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ
    ࠓ೔࿩͢͜ͱ

    View full-size slide

  7. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wίϧʔνϯͱ͸ͳʹ͔
    w,PUMJO͸ͲͷΑ͏ʹίϧʔνϯΛ࣮ݱ
    ͍ͯ͠Δͷ͔
    w,PUMJOίϧʔνϯͷجຊతͳ࢖͍ํ
    ࠓ೔࿩͢͜ͱ

    View full-size slide

  8. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯͱ͸ͳʹ͔

    View full-size slide

  9. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w $PCPMίϯύΠϥΛܰྔʹ࣮
    ૷͢ΔͨΊʹίϧʔνϯͷ֓೦
    Λಋೖͨ͠
    w ͋Δ৚݅ԼͰϓϩάϥϜΛ෼཭
    ͢Δͱಠཱͯͦ͠ΕͧΕಈ࡞Ͱ
    ͖Δͱ͍͏ΞΠσΞ
    w DPSPVUJOFɺͭ·Γର౳ͷϧʔ
    νϯͱ͍͏ҙຯɻ͓ޓ͍Λݺͼ
    ग़੍ͯ͠ޚͰ͖Δؔ܎
    ϝϧϰΟϯɾίϯ΢ΣΠ
    ͷ೥ͷ࿦จ͕ॳग़
    IUUQNFMDPOXBZDPN)PNFQEGDPNQJMFSQEG

    View full-size slide

  10. ©2018 Lang-8 Inc. ALL Rights Reserved.
    "GUFSZFBSTʜ

    View full-size slide

  11. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w "DPSPVUJOFŠJTBOJOTUBODFPGTVTQFOEBCMF
    DPNQVUBUJPO*UJTDPODFQUVBMMZTJNJMBSUPB
    UISFBE JOUIFTFOTFUIBUJUUBLFTBCMPDLPG
    DPEFUPSVOBOEIBTBTJNJMBSMJGFDZDMFŠJU
    JTDSFBUFEBOETUBSUFE CVUJUJTOPUCPVOEUP
    BOZQBSUJDVMBSUISFBE*UNBZTVTQFOEJUT
    FYFDVUJPOJOPOFUISFBEBOESFTVNFJOBOPUIFS
    POF.PSFPWFS MJLFBGVUVSFPSQSPNJTF JU
    NBZDPNQMFUFXJUITPNFSFTVMUPSFYDFQUJPO
    $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ
    IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ

    View full-size slide

  12. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w ίϧʔνϯ͸ɺҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯεͰ͢ɻͦ
    Ε͸֓೦తʹ͸εϨουʹࣅ͍ͯ·͢ɻͭ·Γɺ࣮ߦ͢Δίʔ
    υϒϩοΫΛ࣋ͪɺಉ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Ε
    ىಈ͞Ε·͕͢ɺಛఆͷεϨουʹଋറ͞Ε͍ͯ·ͤΜɻ͋
    ΔεϨουͰ࣮ߦΛதஅ͠ɺผͷεϨουͰ࠶։͢Δ͜ͱ͕
    ͋Γ·͢ɻ͞Βʹɺ'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔
    ͷ݁Ռ΍ྫ֎͕͋Δ৔߹͕͋Γ·͢ɻ
    $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ
    IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ

    View full-size slide

  13. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w ίϧʔνϯ͸ɺҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯεͰ͢ɻͦ
    Ε͸֓೦తʹ͸εϨουʹࣅ͍ͯ·͢ɻͭ·Γɺ࣮ߦ͢Δίʔ
    υϒϩοΫΛ࣋ͪɺಉ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Ε
    ىಈ͞Ε·͕͢ɺಛఆͷεϨουʹଋറ͞Ε͍ͯ·ͤΜɻ͋
    ΔεϨουͰ࣮ߦΛதஅ͠ɺผͷεϨουͰ࠶։͢Δ͜ͱ͕
    ͋Γ·͢ɻ͞Βʹɺ'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔
    ͷ݁Ռ΍ྫ֎͕͋Δ৔߹͕͋Γ·͢ɻ
    $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ
    IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ

    View full-size slide

  14. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wαϒϧʔνϯͱҟͳΓɺঢ়ଶ؅ཧΛ
    ҙࣝͤͣʹߦ͑ΔͨΊɺڠௐతॲཧɺ
    ΠςϨʔλɺແݶϦετɺύΠϓͳ
    Ͳɺܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰
    қʹهड़Ͱ͖Δɻ
    8JLJQFEJBʹΑΔͱʜ
    IUUQTKBXJLJQFEJBPSHXJLJ&#&"#&#$&&#

    View full-size slide

  15. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wαϒϧʔνϯͱҟͳΓɺঢ়ଶ؅ཧΛ
    ҙࣝͤͣʹߦ͑ΔͨΊɺڠௐతॲཧɺ
    ΠςϨʔλɺແݶϦετɺύΠϓͳ
    Ͳɺܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰
    қʹهड़Ͱ͖Δɻ
    8JLJQFEJBʹΑΔͱʜ
    IUUQTKBXJLJQFEJBPSHXJLJ&#&"#&#$&&#

    View full-size slide

  16. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε
    wεϨουͷΑ͏ʹ࣮ߦ͢ΔίʔυϒϩοΫΛ࣋ͪɺಉ
    ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Εىಈ͞ΕΔ͕ɺ
    ಛఆͷεϨουʹଋറ͞Εͳ͍
    w'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔ͷ݁Ռ΍ྫ֎
    ͕͋Δ৔߹͕͋Δ
    wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖Δ
    ίϧʔνϯͱ͸ʜ

    View full-size slide

  17. ©2018 Lang-8 Inc. ALL Rights Reserved.
    Ұ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε
    fun loadProfile(id: Int) {
    val profile = getProfile(id)
    showProfile(profile)
    }
    αϒϧʔνϯ
    ίϧʔνϯ
    ։͔࢝ΒϦλʔϯ·Ͱ͕Ұ
    ͭͷॲཧ୯Ґ
    தஅ
    ࠶։
    ॲཧͷ్தͰҰ࣌ఀࢭ
    ͕Մೳ
    launch {
    val profile = getProfile(id).await()
    showProfile(profile)
    }

    View full-size slide

  18. ©2018 Lang-8 Inc. ALL Rights Reserved.
    εϨουͱίϧʔνϯ
    Thread {
    val profile = getProfile(token)
    showProfile(profile)
    }.start()
    εϨου
    ίϧʔνϯ
    ίʔυϒϩοΫͱϥΠϑα
    ΠΫϧΛ࣋ͪɺ࡞੒ͱىಈ
    ͕͞ΕΔ
    εϨουʹࣅ͍ͯΔ͕
    ಛఆͷεϨουʹଋറ
    ͞Εͳ͍
    launch {
    val profile = getProfile(token).await()
    showProfile(profile)
    }
    ͲͷεϨουͰ࣮ߦ͞ΕΔ͔͸ίϧʔνϯࣗ਎͸ؾʹ͠ͳ͍

    View full-size slide

  19. ©2018 Lang-8 Inc. ALL Rights Reserved.
    'VUVSF΍1SPNJTTͷΑ͏ʹ஋Λฦ͢৔߹͕͋Δ
    fun getProfile(id: Int) = async {
    //…
    return profile
    }
    launch {
    val profile = getProfile(id).await()
    showProfile(profile)
    }

    View full-size slide

  20. ©2018 Lang-8 Inc. ALL Rights Reserved.
    'VUVSF΍1SPNJTTͷΑ͏ʹ஋Λฦ͢৔߹͕͋Δ
    ஋Λฦ͢ίϧʔνϯ
    fun getProfile(id: Int) = async {
    //…
    return profile
    }
    launch {
    val profile = getProfile(id).await()
    showProfile(profile)
    }

    View full-size slide

  21. ©2018 Lang-8 Inc. ALL Rights Reserved.
    'VUVSF΍1SPNJTTͷΑ͏ʹ஋Λฦ͢৔߹͕͋Δ
    fun getProfile(id: Int) = async {
    //…
    return profile
    }
    launch {
    val profile = getProfile(id).await()
    showProfile(profile)
    } ผͷίϧʔνϯΛىಈͯࣗ͠਎͸தஅ
    ͠ɺ݁ՌΛड͚औͬͯ࠶։͢Δ

    View full-size slide

  22. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕
    ༰қʹهड़Ͱ͖Δ

    View full-size slide

  23. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun loadProfile(id: Int) {
    val profile = getProfile(id)
    showProfile(profile)
    }

    View full-size slide

  24. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    ஞ࣮࣍ߦ
    fun loadProfile(id: Int) {
    val profile = getProfile(id)
    showProfile(profile)
    }

    View full-size slide

  25. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    ௨৴ॲཧ΍%#ΞΫηεɺॏ͍ͨܭࢉͳͲΛ
    ߦ͏৔߹ɺϒϩοΩϯάͯ͠͠·͏ɻ
    fun loadProfile(id: Int) {
    val profile = getProfile(id)
    showProfile(profile)
    }

    View full-size slide

  26. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun getProfile(id: Int, f: (Profile) -> Unit)
    fun loadProfile(id: Int) {
    val profile = getProfile(id)
    showProfile(profile)
    } ίʔϧόοΫΛड͚औΔܗʹͯ͠ʜ

    View full-size slide

  27. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun getProfile(id: Int, f: (Profile) -> Unit)
    fun loadProfile(id: Int) {
    getProfile(token) { profile ->
    showProgile(profile)
    }
    }
    ϒϩοΩϯά͠ͳ͍ܗʹ͢Δ

    View full-size slide

  28. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun getProfile(id: Int, f: (Profile) -> Unit)
    fun loadProfile(id: Int) {
    getProfile(token) { profile ->
    showProgile(profile)
    }
    }

    View full-size slide

  29. ©2018 Lang-8 Inc. ALL Rights Reserved.
    お分かりいただけただろうか・・・

    View full-size slide

  30. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun getProfile(id: Int, f: (Profile) -> Unit)
    fun loadProfile(id: Int) {
    getProfile(token) { profile ->
    showProgile(profile)
    }
    }

    View full-size slide

  31. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun getProfile(id: Int, f: (Profile) -> Unit)
    fun loadProfile(id: Int) {
    getProfile(token) { profile ->
    showProgile(profile)
    }
    }
    ؔ਺͸͜͜ͰҰ౓தஅ͍ͯ͠Δʂ

    View full-size slide

  32. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun getProfile(id: Int, f: (Profile) -> Unit)
    fun loadProfile(id: Int) {
    getProfile(token) { profile ->
    showProgile(profile)
    }
    }
    ࠶։͍ͯ͠Δʜ

    View full-size slide

  33. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸
    fun getProfile(id: Int, f: (Profile) -> Unit)
    fun loadProfile(id: Int) {
    getProfile(token) { profile ->
    showProgile(profile)
    }
    }
    ܧଓঢ়گΛ࣋ͭϓϩάϥϜͬ

    View full-size slide

  34. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ༰қʹॻ͘
    fun loadProfile(id: Int) {
    val profile = getProfile(id)
    showProfile(profile)
    }

    View full-size slide

  35. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ༰қʹॻ͘
    fun loadProfile(id: Int) {
    val profile = getProfile(id).await()
    showProfile(profile)
    }
    ˞ٖࣅίʔυͰ͢

    View full-size slide

  36. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ༰қʹॻ͘
    fun loadProfile(id: Int) {
    val profile = getProfile(id).await()
    showProfile(profile)
    }
    ˞ٖࣅίʔυͰ͢
    HFU1SPpMFؔ਺͕ฦ͢ίϧʔν
    ϯʹ੍ޚΛ౉ࣗ͠਎͸தஅ͢Δ

    View full-size slide

  37. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ༰қʹॻ͘
    fun loadProfile(id: Int) {
    val profile = getProfile(id).await()
    showProfile(profile)
    }
    ˞ٖࣅίʔυͰ͢
    HFU1SPpMFؔ਺͕ฦ͢ίϧʔν
    ϯ͸ผͷεϨουͰಈ࡞͍ͯ͠
    Δ͔΋͠Εͳ͍ͯ͠͠ͳ͍͔΋
    ͠Εͳ͍

    View full-size slide

  38. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ༰қʹॻ͘
    fun loadProfile(id: Int) {
    val profile = getProfile(id).await()
    showProfile(profile)
    }
    ˞ٖࣅίʔυͰ͢
    HFU1SPpMFؔ਺͕׬ྃͨ͠Β஋
    Λड͚औΔͱ͜Ζ͔Β࠶։

    View full-size slide

  39. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گΛ༰қʹॻ͘
    ίϧʔνϯ BTZODBXBJU

    fun loadProfile(id: Int) {
    getProfile(token) { profile ->
    showProgile(profile)
    }
    }
    ίʔϧόοΫελΠϧ
    fun loadProfile(id: Int) {
    val profile = getProfile(id).await()
    showProfile(profile)
    } ˞ٖࣅίʔυͰ͢

    View full-size slide

  40. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گ͕૿͑ͯ΋
    fun loadProfile(id: Int) {
    getProfile(id) { profile ->
    getReport(profile.id) { report ->
    //..
    }
    }
    }
    }
    }
    }
    ίʔϧόοΫελΠϧ

    View full-size slide

  41. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓঢ়گ͕૿͑ͯ΋
    ίϧʔνϯ BTZODBXBJU

    fun loadProfile(id: Int) {
    val profile = getProfile(id).await()
    val report = getReport(profile.id).await()
    val a = getA().await()
    val b = getB().await()
    val c = getC().await()
    //…
    }
    ˞ٖࣅίʔυͰ͢

    View full-size slide

  42. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wδΣωϨʔλɺετϦʔϜ
    wνϟϯωϧ
    wΞΫλʔϞσϧ
    wύΠϓϥΠϯ
    wFUDʜ
    ܧଓঢ়گΛ؆୯ʹॻ͚Δ͜ͱͰ
    ࣮૷ָ͕ʹͳΔ΋ͷͨͪ

    View full-size slide

  43. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε
    wεϨουͷΑ͏ʹ࣮ߦ͢ΔίʔυϒϩοΫΛ࣋ͪɺಉ
    ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Εىಈ͞ΕΔ͕ɺ
    ಛఆͷεϨουʹଋറ͞Εͳ͍
    w'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔ͷ݁Ռ΍ྫ֎
    ͕͋Δ৔߹͕͋Δ
    wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖Δ
    ίϧʔνϯͱ͸ʜ

    View full-size slide

  44. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJO͸ͲͷΑ͏ʹ
    ίϧʔνϯΛ࣮ݱ͍ͯ͠Δͷ͔ʁ

    View full-size slide

  45. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJO͸ͲͷΑ͏ʹ
    ίϧʔνϯΛ࣮ݱ͍ͯ͠Δͷ͔ʁ
    fun loadProfile(id: Int) {
    val profile = getProfile(id).await()
    showProfile(profile)
    }
    )PX

    View full-size slide

  46. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJO͸+7.ݴޠ

    View full-size slide

  47. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJOίϯύΠϥ͸
    +BWBόΠτίʔυΛग़ྗ͢
    Δ

    View full-size slide

  48. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ͭ·Γ,PUMJOͷίϧʔνϯ
    ͸+BWBͰ࣮૷Ͱ͖Δܗʹ
    ͳ͍ͬͯΔ͸ͣ

    View full-size slide

  49. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJO͸ͲͷΑ͏ʹ
    ίϧʔνϯΛ+BWBͰ࣮ݱͯ͠
    ͍Δͷ͔ʁ

    View full-size slide

  50. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯΛ
    εςʔτϚγϯʹม׵͍ͯ͠Δ
    IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNE

    View full-size slide

  51. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯΛεςʔτϚγϯʹ͢Δ
    fun simpleCoroutine() {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }

    View full-size slide

  52. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯΛεςʔτϚγϯʹ͢Δ
    fun simpleCoroutine() {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ͜͜Ͱதஅ͢Δͱ͢Δ

    View full-size slide

  53. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯΛεςʔτϚγϯʹ͢Δ
    fun simpleCoroutine() {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ̎ͭͷঢ়ଶʹ෼ղͰ͖Δ
    ̎ͭͷঢ়ଶʹ෼ղͰ͖Δ

    View full-size slide

  54. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯΛεςʔτϚγϯʹ͢Δ
    fun simpleCoroutine() {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ̎ͭͷঢ়ଶʹ෼ղͰ͖Δ
    ԣஅతʹར༻͢Δม਺

    View full-size slide

  55. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯΛεςʔτϚγϯʹ͢Δ
    class SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTimeMillis()
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.currentTimeMillis() - start}”)
    label++
    }
    }
    }
    }

    View full-size slide

  56. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }

    View full-size slide

  57. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }
    ̎ͭͷঢ়ଶΛ࣋ͭ
    ̎ͭͷঢ়ଶΛ࣋ͭ

    View full-size slide

  58. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ݱࡏͷঢ়ଶΛ࣋ͭ
    ݱࡏͷঢ়ଶΛද͢
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }

    View full-size slide

  59. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }
    ݱࡏΛมԽͤ͞Δ

    View full-size slide

  60. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ڞ௨ͯ͠࢖͏ม਺
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }

    View full-size slide

  61. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ॲཧΛ࠶։͢Δؔ਺
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }

    View full-size slide

  62. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }

    View full-size slide

  63. ©2018 Lang-8 Inc. ALL Rights Reserved.
    fun delay(delayTime: Long, coroutine: SimpleCoroutine) {
    Thread {
    Thread.sleep(delayTime)
    coroutine.resume()
    }.start()
    }

    View full-size slide

  64. ©2018 Lang-8 Inc. ALL Rights Reserved.
    fun delay(delayTime: Long, coroutine: SimpleCoroutine) {
    Thread {
    Thread.sleep(delayTime)
    coroutine.resume()
    }.start()
    }
    ࠶։ͷͨΊʹ4JNQMF$PSPVUJOF
    Λड͚औ͓ͬͯ͘

    View full-size slide

  65. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ࢦఆ͞Εͨ࣌ؒεϦʔϓͯ͠
    4JNQMF$PSPVUJOFΛ࠶։͢Δ
    fun delay(delayTime: Long, coroutine: SimpleCoroutine) {
    Thread {
    Thread.sleep(delayTime)
    coroutine.resume()
    }.start()
    }

    View full-size slide

  66. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ass SimpleCoroutine {
    var label = 0
    var start = 0L
    fun resume() {
    when (label) {
    0 -> {
    start = System.currentTi
    println(“start")
    label++
    delay(1000, this)
    }
    1 -> {
    println("end ${System.cu
    label++
    }
    }
    }

    View full-size slide

  67. ©2018 Lang-8 Inc. ALL Rights Reserved.
    val simpleCoroutine = SimpleCoroutine()
    simpleCoroutine.resume()
    fun simpleCoroutine() {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ίϧʔνϯ
    εςʔτϚγϯ

    View full-size slide

  68. ©2018 Lang-8 Inc. ALL Rights Reserved.
    val simpleCoroutine = SimpleCoroutine()
    simpleCoroutine.resume()
    fun simpleCoroutine() {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ίϧʔνϯ
    εςʔτϚγϯ
    TUBSU
    FOE
    TUBSU
    FOE

    View full-size slide

  69. ©2018 Lang-8 Inc. ALL Rights Reserved.
    εςʔτϚγϯͰίϧʔνϯΛ࣮ݱ͢Δ
    wதஅͱ࠶։Λঢ়ଶભҠͱݟཱͯΔ
    w࠶։ʹඞཁͳ৘ใΛεςʔτϚγϯ಺ʹ
    Ωϟϓνϟ͢Δ
    w಺෦ঢ়ଶΛมԽͤ͞ͳ͕Β࣮ߦ͢Δ

    View full-size slide

  70. ©2018 Lang-8 Inc. ALL Rights Reserved.
    Ͳ͏΍ͬͯίϧʔνϯΛ
    εςʔτϚγϯʹม׵͢Δͷ͔

    View full-size slide

  71. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEम০ࢠͱ
    ܧଓ

    View full-size slide

  72. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wίϧʔνϯΛϚʔΩϯά͢ΔͨΊʹTVTQFOEम
    ০ࢠΛಋೖ
    wϥϜμࣜʹTVTQFOEम০ࢠΛ෇༩͢Δͱίϧʔ
    νϯຊମΛද͢ TVTQFOEϥϜμ

    wؔ਺ʹ෇༩͢Δͱதஅ͢Δ৔ॴΛද͢ɻͦͷؔ਺Λ
    TVTQFOEؔ਺ͱݺͿ
    wTVTQFOEؔ਺͸TVTQFOEؔ਺ϥϜμ͔Β͔͠
    ݺͼग़ͤͳ͍
    TVTQFOEम০ࢠ

    View full-size slide

  73. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wίϧʔνϯࣗ਎ΛܧଓΦϒδΣΫτʹ͢Δ
    wTVTQFOEؔ਺Λܧଓ౉͠ελΠϧ
    $POUJOVBUJPO1BTTJOH4UZMF $14

    ʹม׵͢Δ
    wTVTQFOEؔ਺಺ͰܧଓΦϒδΣΫτΛऔΓ
    ग़ͯ͠೚ҙʹ࠶։Ͱ͖ΔΑ͏ʹ͢Δ
    ܧଓ

    View full-size slide

  74. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺ͱ$14
    fun delay(delayTime: Long, coroutine: SimpleCoroutine) {
    Thread {
    Thread.sleep(delayTime)
    coroutine.resume()
    }.start()
    }

    View full-size slide

  75. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺ͱ$14
    suspend fun delay(delayTime: Long) {
    Thread {
    Thread.sleep(delayTime)
    coroutine.resume()
    }.start()
    }

    View full-size slide

  76. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺ͱ$14
    suspend fun delay(delayTime: Long) {
    Thread {
    Thread.sleep(delayTime)
    coroutine.resume()
    }.start()
    }
    // c: Continuation
    ίϯύΠϧ࣌ʹࣗಈͰ
    ܧଓΠϯλϑΣʔεͷ
    Ҿ਺͕૿͑Δ

    View full-size slide

  77. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺ͱ$14
    suspend fun delay(delayTime: Long) { // c: Continuation
    suspendCoroutine { c ->
    Thread {
    Thread.sleep(delayTime)
    c.resume(Unit)
    }.start()
    }
    }

    View full-size slide

  78. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺ͱ$14
    suspend fun delay(delayTime: Long) { // c: Continuation
    suspendCoroutine { c ->
    Thread {
    Thread.sleep(delayTime)
    c.resume(Unit)
    }.start()
    }
    }
    ܧଓΠϯλϑΣʔεΛऔΓग़͢
    ͨΊʹ༻ҙ͞Ε͍ͯΔؔ਺

    View full-size slide

  79. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺ͱ$14
    suspend fun delay(delayTime: Long) { // c: Continuation
    suspendCoroutine { c ->
    Thread {
    Thread.sleep(delayTime)
    c.resume(Unit)
    }.start()
    }
    }
    ܧଓΠϯλϑΣʔεΛ࢖ͬͯ࠶։
    ॲཧΛߦ͏

    View full-size slide

  80. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }

    View full-size slide

  81. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    }

    View full-size slide

  82. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    }
    TVTQFOEϥϜμʹΑͬͯίϧʔνϯͷ
    ൣғ͕ܾ·Δɻ

    View full-size slide

  83. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    f.startCoroutine(NoOpCompletion)
    }

    View full-size slide

  84. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    f.startCoroutine(NoOpCompletion)
    }
    TVTQFOEϥϜμͷ֦ுؔ਺͕͍͔ͭ͘༻
    ҙ͞Ε͍ͯΔɻTUBSU$PSPVUJOFؔ਺ʹΑͬ
    ͯίϧʔνϯΛ։࢝Ͱ͖Δ

    View full-size slide

  85. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    f.startCoroutine(NoOpCompletion)
    }
    TUBSU$PSPVUJOFؔ਺͸$POUJOVBUJPO
    ΛҾ਺ʹऔΔɻίϧʔνϯ͕׬ྃͨ͠Βݺ
    ͼग़͞ΕΔ

    View full-size slide

  86. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    f.startCoroutine(NoOpCompletion)
    }
    ৑௕ʹͳΔͨΊศ্ٓ༻ҙ
    ͨ͠Ϋϥε
    object NoOpCompletion : Continuation {
    override val context: CoroutineContext = EmptyCoroutineContext
    override fun resume(value: T) {
    // no op
    }
    override fun resumeWithException(exception: Throwable) {
    // no op
    }

    View full-size slide

  87. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    f.startCoroutine(NoOpCompletion)
    }

    View full-size slide

  88. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEϥϜμͱίϧʔνϯ
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    f.startCoroutine(NoOpCompletion)
    }
    TUBSU
    FOE

    View full-size slide

  89. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯϏϧμʔͱ
    ܧଓΠϯλʔηϓλʔ

    View full-size slide

  90. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯΛ࡞Δͷ͸ΊΜͲ͍͘͞
    fun simpleCoroutine() {
    val f: suspend () -> Unit = {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    f.startCoroutine(NoOpCompletion)
    }

    View full-size slide

  91. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯϏϧμʔ
    fun simpleCoroutine() {
    launch {
    val start = System.currentTimeMillis()
    println(“start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}”)
    }
    }
    ͦΕͧΕͷੑ࣭Λ΋ͭίϧʔνϯϏϧμʔ
    ͕ίϧʔνϯඪ४ϥΠϒϥϦͰఏڙ͞ΕΔ
    ͷͰීஈ͸ͦͪΒΛ࢖͏͜ͱʹͳΔɻ

    View full-size slide

  92. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓΠϯλʔηϓλʔͱ
    ࣮ߦεϨου
    launch(CommonPool) {
    // …
    }
    launch(UI) {
    // …
    }
    launch(JavaFx) {
    // …
    }
    launch(Swing) {
    // …
    }
    async(CommonPool) {
    // …
    }

    View full-size slide

  93. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ܧଓΠϯλʔηϓλʔͱ
    ࣮ߦεϨου
    launch(CommonPool) {
    // …
    }
    launch(UI) {
    // …
    }
    launch(JavaFx) {
    // …
    }
    launch(Swing) {
    // …
    }
    async(CommonPool) {
    // …
    }
    ίϧʔνϯϏϧμʔʹܧଓΠϯ
    λʔηϓλʔΛ౉࣮ͯ͠ߦεϨο
    υΛίϯτϩʔϧͰ͖Δ

    View full-size slide

  94. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wίϧʔνϯΛεςʔτϚγϯʹม׵͠
    ͍ͯΔ
    wม׵ͷͨΊʹTVTQFOEम০ࢠͱܧ
    ଓΛಋೖͨ͠
    wܧଓΠϯλʔηϓλʔʹΑΓɺ࣮ߦͷ
    εϨουΛݶఆ͠ͳ͍
    ,PUMJO͸ͲͷΑ͏ʹ
    ίϧʔνϯΛ+BWBͰ࣮ݱ͍ͯ͠Δͷ͔ʁ

    View full-size slide

  95. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJOίϧʔνϯͷ
    جຊతͳ࢖͍ํ

    View full-size slide

  96. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJO͔Βొ৔

    View full-size slide

  97. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ֤छػೳ͸ϥΠϒϥϦͰఏڙ
    wDPSF SFBDUJWF OBUJWF VJ KTͳͲ
    ͷϞδϡʔϧʹ෼͔Ε͍ͯΔ
    IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT

    View full-size slide

  98. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ಛఆͷ؀ڥ޲͚ͷϥΠϒϥϦ΋͋Δ
    w"OESPJE +BWB'Y 4XJOH޲͚͕༻ҙ͞
    Ε͍ͯΔ ܧଓΠϯλʔηϓλʔͷ࣮૷

    IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTUSFFNBTUFSVJ

    View full-size slide

  99. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wLPUMJOYDPSPVUJOFTFYQFSJNFOUBMύοέʔ
    δʹؔ࿈͢ΔΫϥε͕͋Δ
    wຊ൪౤ೖʹ͸ͦΕͳΓʹ֮ޛ͕ඞཁ͕ͩͬͨ
    FYQFSJNFOUBM࿮Ͱఏڙ

    View full-size slide

  100. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ͰTUBCMFʹͳΔ༧ఆʂʂ

    View full-size slide

  101. ©2018 Lang-8 Inc. ALL Rights Reserved.

    View full-size slide

  102. ©2018 Lang-8 Inc. ALL Rights Reserved.

    ˞ࠓճ͸·ͩFYQFSJNFOUBMͷ؀ڥͷํͰղઆ͠·͢

    View full-size slide

  103. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ؀ڥߏங
    kotlin {
    experimental {
    coroutines 'enable'
    }
    }
    dependencies {
    implementation”org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.60"
    implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0"
    // platformʹ߹Θͤͯ௥Ճ
    implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0"
    }
    BQQCVJMEHSBEMF

    View full-size slide

  104. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ؀ڥߏங
    kotlin {
    experimental {
    coroutines 'enable'
    }
    }
    dependencies {
    implementation”org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.60"
    implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0"
    // platformʹ߹Θͤͯ௥Ճ
    implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0"
    }
    BQQCVJMEHSBEMF
    ແͯ͘΋ಈ͚͘Ͳ*%&্ͱ
    ͔Ͱܯࠂ͕ͰΔ

    View full-size slide

  105. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $PSFϥΠϒϥϦ͕ఏڙ͢ΔίϧʔνϯϏϧμʔ

    View full-size slide

  106. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $PSFϥΠϒϥϦ͕ఏڙ͢ΔίϧʔνϯϏϧμʔ

    View full-size slide

  107. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ࠷ॳͷίϧʔνϯ
    launch {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }

    View full-size slide

  108. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ࠷ॳͷίϧʔνϯ
    launch {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ίϧʔνϯϏϧμʔ

    View full-size slide

  109. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w݁ՌΛ࣋ͨͳ͍ίϧʔνϯΛ࡞੒͢Δί
    ϧʔνϯϏϧμʔؔ਺
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job

    View full-size slide

  110. ©2018 Lang-8 Inc. ALL Rights Reserved.
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job
    ίϧʔνϯຊମɻ$PSPVUJOF4DPQFͷ֦ு
    TVTQFOEϥϜμ

    View full-size slide

  111. ©2018 Lang-8 Inc. ALL Rights Reserved.
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job
    ίϧʔνϯͷ࣮ߦίϯςΩετɺڞ༗σʔ
    λ΍ܧଓΠϯλʔηϓλʔͳͲΛ͜͜Ͱࢦ
    ఆͰ͖Δ

    View full-size slide

  112. ©2018 Lang-8 Inc. ALL Rights Reserved.
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job

    View full-size slide

  113. ©2018 Lang-8 Inc. ALL Rights Reserved.
    %FGBVMU%JTQBUDIFS
    public actual val DefaultDispatcher: CoroutineDispatcher =
    if (useCoroutinesScheduler) ExperimentalCoroutineDispatcher() else CommonPool

    View full-size slide

  114. ©2018 Lang-8 Inc. ALL Rights Reserved.
    %FGBVMU%JTQBUDIFS
    tcher() else CommonPool

    View full-size slide

  115. ©2018 Lang-8 Inc. ALL Rights Reserved.
    %FGBVMU%JTQBUDIFS
    tcher() else CommonPool
    εϨουϓʔϧΛ࢖ͬͯίϧʔνϯΛ࣮ߦ͢Δ
    ܧଓΠϯλʔηϓλʔɻ࣮ߦ؀ڥͷ$16ίΞ
    ਺ʹԠͯ͡εϨου਺͸มԽ͢Δ

    View full-size slide

  116. ©2018 Lang-8 Inc. ALL Rights Reserved.
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job
    ίϧʔνϯͷ։࢝ํ๏Λࢦఆ͢ΔɻσϑΥ
    ϧτͰ͸ଈ࠲ʹ։࢝͢Δ

    View full-size slide

  117. ©2018 Lang-8 Inc. ALL Rights Reserved.
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job
    ىಈͨ͠ίϧʔνϯͷ׬ྃΛ଴ͪ߹Θͤͨ
    ΓΩϟϯηϧͰ͖Δ

    View full-size slide

  118. ©2018 Lang-8 Inc. ALL Rights Reserved.
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job
    ਌+PCΛઃఆ͢Δɻෳ਺ͷίϧʔνϯ
    ΛҰׅͰDBODFM͍ͨ࣌͠ͳͲʹ࢖͏

    View full-size slide

  119. ©2018 Lang-8 Inc. ALL Rights Reserved.
    MBVODIؔ਺
    public fun launch(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> Unit
    ): Job
    ίϧʔνϯͷ׬ྃΛίʔϧόοΫͰड͚औ
    Γ͍ͨ৔߹ʹࢦఆ͢Δ

    View full-size slide

  120. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ࠷ॳͷίϧʔνϯ
    launch {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }

    View full-size slide

  121. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ࠷ॳͷίϧʔνϯ
    launch {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ඪ४ϥΠϒϥϦ͕ఏڙ͢Δ
    TVTQFOEؔ਺

    View full-size slide

  122. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ࠷ॳͷίϧʔνϯ
    launch {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    TUBSU
    FOE

    View full-size slide

  123. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯͱ࣮ߦεϨου
    launch {
    val start = System.currentTimeMillis()
    println("start")
    delay(1000)
    println("end ${System.currentTimeMillis() - start}")
    }
    ͲͪΒͷঢ়ଶ΋εϨουϓʔ
    ϧͰ࣮ߦ͞ΕΔ

    View full-size slide

  124. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ
    launch {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }

    View full-size slide

  125. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ
    launch {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }

    %FGBVMU%JTQBUDIFS͸εϨουϓʔ
    ϧͰίϧʔνϯΛ࣮ߦ͢Δ

    View full-size slide

  126. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ
    launch(UI) {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }

    View full-size slide

  127. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ
    launch(UI) {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }
    LPUMJOYDPSPVUJOFTBOESPJE͕ఏڙ͢Δ
    ܧଓΠϯλʔηϓλʔɻৗʹ6*εϨουͰ࠶։
    ͢Δ

    View full-size slide

  128. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ
    launch(UI) {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }
    6*εϨουͰ࣮ߦ

    6*εϨουͰ࣮ߦ

    View full-size slide

  129. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    launch(UI) {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }

    View full-size slide

  130. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }

    View full-size slide

  131. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    delay(1000)
    prograssBar.isVisible = false
    }
    ٖࣅతͳ௨৴ॲཧ

    View full-size slide

  132. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = loadProfile()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }

    View full-size slide

  133. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = loadProfile()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }
    ϒϩοΩϯά͢Δ

    View full-size slide

  134. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }
    BTZODؔ਺Λ࢖͏

    View full-size slide

  135. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w݁ՌΛ࣋ͭίϧʔνϯΛ࡞੒͢Δίϧʔ
    νϯϏϧμʔؔ਺
    BTZODؔ਺
    public fun async(
    context: CoroutineContext = DefaultDispatcher,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    parent: Job? = null,
    onCompletion: CompletionHandler? = null,
    block: suspend CoroutineScope.() -> T
    ): Deferred

    View full-size slide

  136. ©2018 Lang-8 Inc. ALL Rights Reserved.
    %FGFSSFE5
    public interface Deferred : Job {
    //…
    public suspend fun await(): T
    //…
    }
    தஅͯ݁͠ՌΛ଴ͬͯ࠶։͢Δ

    View full-size slide

  137. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }

    View full-size slide

  138. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }

    View full-size slide

  139. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }
    ܧଓΛड͚औͬͯॲཧͷ׬ྃΛ଴ͭ

    View full-size slide

  140. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    Thread.sleep(1000)
    return Profile()
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }
    ඇಉظॲཧ͕׬ྃ͢Δͱɺ6*εϨο
    υͰܧଓΛ࠶։͢Δ

    View full-size slide

  141. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJU
    class Profile(val name: String = "Jack")
    fun loadProfile(): Profile {
    val profile = http.get(“http:/.”)
    return profile
    }
    launch(UI) {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }

    View full-size slide

  142. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wMBVODIؔ਺͸݁Ռ͕ແ͍ίϧʔνϯΛɺBTZOD
    ؔ਺͸݁Ռ͕͋ΔίϧʔνϯΛ࡞Δ
    wσϑΥϧτͰ͸$PNNPO1PPM͕࢖ΘΕΔͷͰ
    ඞཁʹԠͯ͡ܧଓΠϯλʔηϓλʔΛηοτ͢
    Δ
    wMBVODIؔ਺ͱBTZODؔ਺Λ૊Έ߹Θͤͯ࢖͏
    େମ͜ΕͰࣄ଍ΓΔ

    ίϧʔνϯͷجຊతͳ࢖͍ํ

    View full-size slide

  143. ©2018 Lang-8 Inc. ALL Rights Reserved.
    BTZODBXBJUͱඇಉظॲཧ

    View full-size slide

  144. ©2018 Lang-8 Inc. ALL Rights Reserved.
    جຊܗ
    launch(UI) {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    }

    View full-size slide

  145. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ΤϥʔϋϯυϦϯά
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }

    View full-size slide

  146. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }

    View full-size slide

  147. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val token = async { getToken() }
    val profile = async { loadProfile(token.await()) }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }

    View full-size slide

  148. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val token = async { getToken() }
    val profile = async { loadProfile(token.await()) }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }
    ͜͜Ͱ͸தஅͤͣ։࢝
    ͚͍ͩͯ͠Δ

    View full-size slide

  149. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛ௚ྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val token = async { getToken() }
    val profile = async { loadProfile(token.await()) }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }
    ͜͜Ͱ݁ՌΛ଴͍ͬͯΔ

    View full-size slide

  150. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛฒྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    val articles = async { loadArticles() }.await()
    show(profile, articles)
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }

    View full-size slide

  151. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛฒྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    val articles = async { loadArticles() }.await()
    show(profile, articles)
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }
    ௚ྻͰ࣮ߦ͞Εͯ͠·͏

    View full-size slide

  152. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛฒྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val profile = async { loadProfile() }
    val articles = async { loadArticles() }
    show(profile.await(), articles.await())
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }

    View full-size slide

  153. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦΫΤετΛฒྻͰ࣮ߦ͢Δ
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val profile = async { loadProfile() }
    val articles = async { loadArticles() }
    show(profile.await(), articles.await())
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }
    ฒྻͰ։͓͍࢝ͯͯ͠ɺ଴ͪ߹Θͤ
    Δͱ͜ΖͰҰؾʹBXBJU͢Δ

    View full-size slide

  154. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $BODFM
    launch(UI) {
    ɹtry{
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }

    View full-size slide

  155. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $BODFM
    val job = launch(UI) {
    ɹtry {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: CancellationException) {
    // cancel
    } catch (e: Exception) {
    showError(e)
    }
    }
    //…
    job.cancel()

    View full-size slide

  156. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $BODFM
    val job = launch(UI) {
    ɹtry {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: CancellationException) {
    // cancel
    } catch (e: Exception) {
    showError(e)
    }
    }
    //…
    job.cancel()
    ίϧʔνϯΛىಈ͢Δͱ͖ʹ+PCΛ
    ड͚औ͓͚ͬͯ͹೚ҙͷλΠϛϯά
    ͰΩϟϯηϧ͕Ͱ͖Δ

    View full-size slide

  157. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $BODFM
    val job = launch(UI) {
    ɹtry {
    prograssBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    prograssBar.isVisible = false
    } catch (e: CancellationException) {
    // cancel
    } catch (e: Exception) {
    showError(e)
    }
    }
    //…
    job.cancel()
    Ωϟϯηϧͷྫ֎͕ඈΜͰ͘ΔͷͰ
    Ωϟον

    View full-size slide

  158. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $BODFM "OESPJEͷྫ

    class MainActivity : AppCompatActivity() {
    var job: Job? = null
    //…
    fun doSomething(){
    job = launch(UI) { …
    }
    //…
    override fun onPause() {
    job?.cancel()
    super.onPause()
    }
    }

    View full-size slide

  159. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $BODFM "OESPJEͷྫ

    class MainActivity : AppCompatActivity() {
    var job: Job? = null
    //…
    fun doSomething(){
    job = launch(UI) { …
    }
    //…
    override fun onPause() {
    job?.cancel()
    super.onPause()
    }
    }
    ϥΠϑαΠΫϧʹ߹Θͤ
    ͯΩϟϯηϧ

    View full-size slide

  160. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ·ͱΊͯ$BODFM
    class MainActivity : AppCompatActivity() {
    var rootJob: Job? = null
    //…
    fun doSomething(){
    launch(UI, parent = rootJob) { …
    }
    fun doSomething2(){
    launch(UI, parent = rootJob) { …
    }
    //…
    override fun onResume() {
    super.onResume()
    rootJob = Job()
    }
    override fun onPause() {
    rootJob.cancel()
    super.onPause()
    }

    View full-size slide

  161. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ·ͱΊͯ$BODFM
    class MainActivity : AppCompatActivity() {
    var rootJob: Job? = null
    override fun onResume() {
    super.onResume()
    rootJob = Job()
    }
    //…
    fun doSomething(){
    launch(UI, parent = rootJob) { …
    }
    fun doSomething2(){
    launch(UI, parent = rootJob) { …
    }
    //…
    override fun onPause() {
    rootJob.cancel()
    super.onPause()
    }

    View full-size slide

  162. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ·ͱΊͯ$BODFM
    class MainActivity : AppCompatActivity() {
    var rootJob: Job? = null
    override fun onResume() {
    super.onResume()
    rootJob = Job()
    }
    //…
    fun doSomething(){
    launch(UI, parent = rootJob) { …
    }
    fun doSomething2(){
    launch(UI, parent = rootJob) { …
    }
    //…
    override fun onPause() {
    rootJob.cancel()
    super.onPause()
    }

    View full-size slide

  163. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ·ͱΊͯ$BODFM
    class MainActivity : AppCompatActivity() {
    var rootJob: Job? = null
    override fun onResume() {
    super.onResume()
    rootJob = Job()
    }
    //…
    fun doSomething(){
    launch(UI, parent = rootJob) { …
    }
    fun doSomething2(){
    launch(UI, parent = rootJob) { …
    }
    //…
    override fun onPause() {
    rootJob?.cancel()
    super.onPause()
    }
    ·ͱΊͯΩϟϯηϧ

    View full-size slide

  164. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦτϥΠ
    launch(UI) {
    try {
    progressBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    progressBar.isVisible = false
    } catch (e: Exception) {
    showError(e)
    }
    }

    View full-size slide

  165. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦτϥΠ
    launch(UI) {
    repeat(3) {
    try {
    progressBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    progressBar.isVisible = false
    return@launch
    } catch (e: Exception) {
    if (!shouldRetry(e)) {
    return@repeat
    }
    }
    }
    showError()
    }

    View full-size slide

  166. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦτϥΠ
    launch(UI) {
    repeat(3) {
    try {
    progressBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    progressBar.isVisible = false
    return@launch
    } catch (e: Exception) {
    if (!shouldRetry(e)) {
    return@repeat
    }
    }
    }
    showError()
    }
    GPSจͱҰॹɻ͜Ε͸ί
    ϧʔνϯͰ͸ͳ͍ɻ

    View full-size slide

  167. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦτϥΠ
    launch(UI) {
    repeat(3) {
    try {
    progressBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    progressBar.isVisible = false
    return@launch
    } catch (e: Exception) {
    if (!shouldRetry(e)) {
    return@repeat
    }
    }
    }
    showError()
    }
    ੒ޭͨ͠ΒMBVODI͔Β
    ൈ͚Δ

    View full-size slide

  168. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦτϥΠ
    launch(UI) {
    repeat(3) {
    try {
    progressBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    progressBar.isVisible = false
    return@launch
    } catch (e: Exception) {
    if (!shouldRetry(e)) {
    return@repeat
    }
    }
    }
    showError()
    }
    ϦτϥΠ͢΂͖ΤϥʔͰͳ
    ͍ͳΒSFQFBUΛൈ͚Δ

    View full-size slide

  169. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ϦτϥΠ
    launch(UI) {
    repeat(3) {
    try {
    progressBar.isVisible = true
    val profile = async { loadProfile() }.await()
    nameText.text = profile.name
    progressBar.isVisible = false
    return@launch
    } catch (e: Exception) {
    if (!shouldRetry(e)) {
    return@repeat
    }
    }
    }
    showError()
    }
    ϦτϥΠ͕શࣦͯഊ͔ͨ͠Ϧτϥ
    Π͢΂͖Ͱͳ͍Τϥʔ͕ൃੜ৔߹

    View full-size slide

  170. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wUSZDBUDIͰΤϥʔॲཧ
    w௚ྻɺฒྻ͸ݺͼग़͠ํͰίϯτϩʔϧ
    w+PCΛ࢖ͬͯΩϟϯηϧ
    wϦτϥΠͳͲ΋ϧʔϓͰॲཧͰ͖Δ
    BTZODBXBJUͱඇಉظॲཧ

    View full-size slide

  171. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ·ͩ·ͩ͋ΔΑίϧʔνϯ

    View full-size slide

  172. ©2018 Lang-8 Inc. ALL Rights Reserved.
    3FUSPpUΛ%FGFSSFEԽ
    IUUQTHJUIVCDPN+BLF8IBSUPOSFUSPpULPUMJODPSPVUJOFTBEBQUFS
    +BLF8IBSUPO੡ɻ$BMM"EBQUFSΛ࣮૷ͯ͠%FGFSSFE
    Λฦ͢Α͏ʹ͍ͯ͠Δɻ
    @POST("/api/v1/articles/{id}/like")
    fun like(@Path(“id") id: Long): Deferred
    @DELETE(“/api/v1/articles/{id}/like")
    fun likeCancel(@Path("id") id: Long): Deferred
    @POST(“/api/v1/users/sign_up”)
    fun signUp(@Body params: SignUpParameter): Deferred
    @POST("/api/v1/users/sign_in")
    fun signIn(@Body params: SignInParameter): Deferred

    View full-size slide

  173. ©2018 Lang-8 Inc. ALL Rights Reserved.
    &WFOU#VTͱ$IBOOFM
    class EventBus {
    private val channel = BroadcastChannel(1)
    fun send(event: Any, context: CoroutineContext = DefaultDispatcher) {
    launch(context) {
    channel.send(event)
    }
    }
    fun subscribe(): ReceiveChannel =
    channel.openSubscription()
    inline fun subscribeToEvent() =
    subscribe().filter { it is T }.map { it as T }
    }
    IUUQTHJTUHJUIVCDPNTWFOKBDPCTBCEEBCDEB

    View full-size slide

  174. ©2018 Lang-8 Inc. ALL Rights Reserved.
    &WFOU#VTͱ$IBOOFM
    val bus: EventBus by inject()
    override fun onResume() {
    super.onResume()
    launch(UI, parent = rootJob) {
    bus.subscribeToEvent().consumeEach {
    // logged out
    }
    }
    }
    override fun onPause() {
    rootJob.cancel()
    super.onPause()
    }
    //…
    bus.send(LogoutEvent)

    View full-size slide

  175. ©2018 Lang-8 Inc. ALL Rights Reserved.
    PO"DUJWJUZ3FTVMUΛTVTQFOE
    launch(UI) {
    val result = activityResult(intent)
    if (result.isOk) {
    // ok
    val data: Intent? = result.flatMap()
    } else {
    // ng
    }
    }
    IUUQTHJUIVCDPNQEWSJF[FBOESPJEDPSPVUJOFT
    BOESPJEDPSPVUJOFTͱ͍͏αʔυύʔςΟϥΠϒϥϦ͕
    ఏڙɻSFRVFTU1FSNJTTJPO΋͋ΔΑ

    View full-size slide

  176. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w ܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖ͯศར
    w ,PUMJO͸ίϧʔνϯΛεςʔτϚγϯʹม׵࣮ͯ͠ݱ͠
    ͍ͯΔ
    w BTZODBXBJUͳͲͷ֤ػೳ͸,PUMJOͷެࣜϥΠϒϥϦ
    ͕ఏڙ͍ͯ͠Δ
    w MBVODIؔ਺ͱBTZODؔ਺Ͱେ఍଍ΓΔ
    w ඞཁʹԠͯࣗ͡෼Ͱίϧʔνϯͷ࢓૊ΈΛ࣮૷Ͱ͖Δɻ͢
    ͰʹαʔυύʔςΟϥΠϒϥϦ͕͍͔ͭ͋͘Δ
    ,PUMJOίϧʔνϯ·ͱΊ

    View full-size slide

  177. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ©2018 Lang-8 Inc. ALL Rights Reserved.
    &OKPZ,PUMJO$PSPVUJOF

    View full-size slide

  178. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ©2018 Lang-8 Inc. ALL Rights Reserved.
    ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide