$30 off During Our Annual Pro Sale. View Details »

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. wീ໦ढ़޿ w!TZTZBHJ w$50PG w"OESPJE&OHJOFFS

    "CPVU.F Jetpack Handbook ʲC94৽ץʳ ʮୈ6ষ WorkManagerͱόοΫάϥ΢ϯυλεΫʯ ॻ͖·ͨ͠
  2. ©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL

    Rights Reserved. https://hinative.com 8FBSFIJSJOH
  3. ©2018 Lang-8 Inc. ALL Rights Reserved. w $PCPMίϯύΠϥΛܰྔʹ࣮ ૷͢ΔͨΊʹίϧʔνϯͷ֓೦ Λಋೖͨ͠

    w ͋Δ৚݅ԼͰϓϩάϥϜΛ෼཭ ͢Δͱಠཱͯͦ͠ΕͧΕಈ࡞Ͱ ͖Δͱ͍͏ΞΠσΞ w DPSPVUJOFɺͭ·Γର౳ͷϧʔ νϯͱ͍͏ҙຯɻ͓ޓ͍Λݺͼ ग़੍ͯ͠ޚͰ͖Δؔ܎ ϝϧϰΟϯɾίϯ΢ΣΠ ͷ೥ͷ࿦จ͕ॳग़ IUUQNFMDPOXBZDPN)PNFQEGDPNQJMFSQEG
  4. ©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
  5. ©2018 Lang-8 Inc. ALL Rights Reserved. w ίϧʔνϯ͸ɺҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯεͰ͢ɻͦ Ε͸֓೦తʹ͸εϨουʹࣅ͍ͯ·͢ɻͭ·Γɺ࣮ߦ͢Δίʔ υϒϩοΫΛ࣋ͪɺಉ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞੒͞Ε

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

    ىಈ͞Ε·͕͢ɺಛఆͷεϨουʹଋറ͞Ε͍ͯ·ͤΜɻ͋ ΔεϨουͰ࣮ߦΛதஅ͠ɺผͷεϨουͰ࠶։͢Δ͜ͱ͕ ͋Γ·͢ɻ͞Βʹɺ'VUVSF΍1SPNJTTͷΑ͏ʹɺԿΒ͔ ͷ݁Ռ΍ྫ֎͕͋Δ৔߹͕͋Γ·͢ɻ $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ
  7. ©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) }
  8. ©2018 Lang-8 Inc. ALL Rights Reserved. εϨουͱίϧʔνϯ Thread { val

    profile = getProfile(token) showProfile(profile) }.start() εϨου ίϧʔνϯ ίʔυϒϩοΫͱϥΠϑα ΠΫϧΛ࣋ͪɺ࡞੒ͱىಈ ͕͞ΕΔ εϨουʹࣅ͍ͯΔ͕ ಛఆͷεϨουʹଋറ ͞Εͳ͍ launch { val profile = getProfile(token).await() showProfile(profile) } ͲͷεϨουͰ࣮ߦ͞ΕΔ͔͸ίϧʔνϯࣗ਎͸ؾʹ͠ͳ͍
  9. ©2018 Lang-8 Inc. ALL Rights Reserved. 'VUVSF΍1SPNJTTͷΑ͏ʹ஋Λฦ͢৔߹͕͋Δ fun getProfile(id: Int)

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

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

    = async { //… return profile } launch { val profile = getProfile(id).await() showProfile(profile) } ผͷίϧʔνϯΛىಈͯࣗ͠਎͸தஅ ͠ɺ݁ՌΛड͚औͬͯ࠶։͢Δ
  12. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

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

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

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

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

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

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

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

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

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

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

    { val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ HFU1SPpMFؔ਺͕׬ྃͨ͠Β஋ Λड͚औΔͱ͜Ζ͔Β࠶։
  23. ©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) } ˞ٖࣅίʔυͰ͢
  24. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گ͕૿͑ͯ΋ fun loadProfile(id: Int)

    { getProfile(id) { profile -> getReport(profile.id) { report -> //.. } } } } } } ίʔϧόοΫελΠϧ
  25. ©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() //… } ˞ٖࣅίʔυͰ͢
  26. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {

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

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

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

    val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ̎ͭͷঢ়ଶʹ෼ղͰ͖Δ ԣஅతʹར༻͢Δม਺
  30. ©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++ } } } }
  31. ©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++ } } }
  32. ©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++ } } } ̎ͭͷঢ়ଶΛ࣋ͭ ̎ͭͷঢ়ଶΛ࣋ͭ
  33. ©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++ } } }
  34. ©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++ } } } ݱࡏΛมԽͤ͞Δ
  35. ©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++ } } }
  36. ©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++ } } }
  37. ©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++ } } }
  38. ©2018 Lang-8 Inc. ALL Rights Reserved. fun delay(delayTime: Long, coroutine:

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

    SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } ࠶։ͷͨΊʹ4JNQMF$PSPVUJOF Λड͚औ͓ͬͯ͘
  40. ©2018 Lang-8 Inc. ALL Rights Reserved. ࢦఆ͞Εͨ࣌ؒεϦʔϓͯ͠ 4JNQMF$PSPVUJOFΛ࠶։͢Δ fun delay(delayTime:

    Long, coroutine: SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() }
  41. ©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++ } } }
  42. ©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}") } ίϧʔνϯ εςʔτϚγϯ
  43. ©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
  44. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯΛϚʔΩϯά͢ΔͨΊʹTVTQFOEम ০ࢠΛಋೖ wϥϜμࣜʹTVTQFOEम০ࢠΛ෇༩͢Δͱίϧʔ νϯຊମΛද͢

    TVTQFOEϥϜμ  wؔ਺ʹ෇༩͢Δͱதஅ͢Δ৔ॴΛද͢ɻͦͷؔ਺Λ TVTQFOEؔ਺ͱݺͿ wTVTQFOEؔ਺͸TVTQFOEؔ਺ϥϜμ͔Β͔͠ ݺͼग़ͤͳ͍ TVTQFOEम০ࢠ
  45. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯࣗ਎ΛܧଓΦϒδΣΫτʹ͢Δ wTVTQFOEؔ਺Λܧଓ౉͠ελΠϧ $POUJOVBUJPO1BTTJOH4UZMF $14

    ʹม׵͢Δ wTVTQFOEؔ਺಺ͰܧଓΦϒδΣΫτΛऔΓ ग़ͯ͠೚ҙʹ࠶։Ͱ͖ΔΑ͏ʹ͢Δ ܧଓ
  46. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ$14 fun delay(delayTime: Long,

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

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

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

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

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

    Long) { // c: Continuation<T> suspendCoroutine<Unit> { c -> Thread { Thread.sleep(delayTime) c.resume(Unit) }.start() } } ܧଓΠϯλϑΣʔεΛ࢖ͬͯ࠶։ ॲཧΛߦ͏
  52. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {

    val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") }
  53. ©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}”) } }
  54. ©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ϥϜμʹΑͬͯίϧʔνϯͷ ൣғ͕ܾ·Δɻ
  55. ©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) }
  56. ©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ؔ਺ʹΑͬ ͯίϧʔνϯΛ։࢝Ͱ͖Δ
  57. ©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 ΛҾ਺ʹऔΔɻίϧʔνϯ͕׬ྃͨ͠Βݺ ͼग़͞ΕΔ
  58. ©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<T> : Continuation<T> { override val context: CoroutineContext = EmptyCoroutineContext override fun resume(value: T) { // no op } override fun resumeWithException(exception: Throwable) { // no op }
  59. ©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) }
  60. ©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
  61. ©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) }
  62. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔ fun simpleCoroutine() {

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

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

    // … } launch(UI) { // … } launch(JavaFx) { // … } launch(Swing) { // … } async(CommonPool) { // … } ίϧʔνϯϏϧμʔʹܧଓΠϯ λʔηϓλʔΛ౉࣮ͯ͠ߦεϨο υΛίϯτϩʔϧͰ͖Δ
  65. ©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯΛεςʔτϚγϯʹม׵͠ ͍ͯΔ wม׵ͷͨΊʹTVTQFOEम০ࢠͱܧ ଓΛಋೖͨ͠

    wܧଓΠϯλʔηϓλʔʹΑΓɺ࣮ߦͷ εϨουΛݶఆ͠ͳ͍ ,PUMJO͸ͲͷΑ͏ʹ ίϧʔνϯΛ+BWBͰ࣮ݱ͍ͯ͠Δͷ͔ʁ
  66. ©2018 Lang-8 Inc. ALL Rights Reserved. ֤छػೳ͸ϥΠϒϥϦͰఏڙ wDPSF SFBDUJWF OBUJWF

    VJ KTͳͲ ͷϞδϡʔϧʹ෼͔Ε͍ͯΔ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT
  67. ©2018 Lang-8 Inc. ALL Rights Reserved. ಛఆͷ؀ڥ޲͚ͷϥΠϒϥϦ΋͋Δ w"OESPJE +BWB'Y 4XJOH޲͚͕༻ҙ͞

    Ε͍ͯΔ ܧଓΠϯλʔηϓλʔͷ࣮૷ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTUSFFNBTUFSVJ
  68. ©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
  69. ©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 ແͯ͘΋ಈ͚͘Ͳ*%&্ͱ ͔Ͱܯࠂ͕ͰΔ
  70. ©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val

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

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ίϧʔνϯϏϧμʔ
  72. ©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
  73. ©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ϥϜμ
  74. ©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 ίϧʔνϯͷ࣮ߦίϯςΩετɺڞ༗σʔ λ΍ܧଓΠϯλʔηϓλʔͳͲΛ͜͜Ͱࢦ ఆͰ͖Δ
  75. ©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
  76. ©2018 Lang-8 Inc. ALL Rights Reserved. %FGBVMU%JTQBUDIFS public actual val

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

    εϨουϓʔϧΛ࢖ͬͯίϧʔνϯΛ࣮ߦ͢Δ ܧଓΠϯλʔηϓλʔɻ࣮ߦ؀ڥͷ$16ίΞ ਺ʹԠͯ͡εϨου਺͸มԽ͢Δ
  78. ©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 ίϧʔνϯͷ։࢝ํ๏Λࢦఆ͢ΔɻσϑΥ ϧτͰ͸ଈ࠲ʹ։࢝͢Δ
  79. ©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 ىಈͨ͠ίϧʔνϯͷ׬ྃΛ଴ͪ߹Θͤͨ ΓΩϟϯηϧͰ͖Δ
  80. ©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͍ͨ࣌͠ͳͲʹ࢖͏
  81. ©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 ίϧʔνϯͷ׬ྃΛίʔϧόοΫͰड͚औ Γ͍ͨ৔߹ʹࢦఆ͢Δ
  82. ©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val

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

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

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } TUBSU FOE
  85. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯͱ࣮ߦεϨου launch { val

    start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ͲͪΒͷঢ়ଶ΋εϨουϓʔ ϧͰ࣮ߦ͞ΕΔ
  86. ©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑͹6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch { prograssBar.isVisible

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

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

    = true delay(1000) prograssBar.isVisible = false } 6*εϨουͰ࣮ߦ 6*εϨουͰ࣮ߦ
  89. ©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 }
  90. ©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 } ٖࣅతͳ௨৴ॲཧ
  91. ©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 }
  92. ©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 } ϒϩοΩϯά͢Δ
  93. ©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ؔ਺Λ࢖͏
  94. ©2018 Lang-8 Inc. ALL Rights Reserved. w݁ՌΛ࣋ͭίϧʔνϯΛ࡞੒͢Δίϧʔ νϯϏϧμʔؔ਺ BTZODؔ਺ public

    fun <T> async( context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> T ): Deferred<T>
  95. ©2018 Lang-8 Inc. ALL Rights Reserved. %FGFSSFE5 public interface Deferred<out

    T> : Job { //… public suspend fun await(): T //… } தஅͯ݁͠ՌΛ଴ͬͯ࠶։͢Δ
  96. ©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 }
  97. ©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 }
  98. ©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 } ܧଓΛड͚औͬͯॲཧͷ׬ྃΛ଴ͭ
  99. ©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*εϨο υͰܧଓΛ࠶։͢Δ
  100. ©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 }
  101. ©2018 Lang-8 Inc. ALL Rights Reserved. جຊܗ launch(UI) { prograssBar.isVisible

    = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false }
  102. ©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) } }
  103. ©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) } }
  104. ©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) } }
  105. ©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) } } ͜͜Ͱ͸தஅͤͣ։࢝ ͚͍ͩͯ͠Δ
  106. ©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) } } ͜͜Ͱ݁ՌΛ଴͍ͬͯΔ
  107. ©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) } }
  108. ©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) } } ௚ྻͰ࣮ߦ͞Εͯ͠·͏
  109. ©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) } }
  110. ©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͢Δ
  111. ©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) } }
  112. ©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()
  113. ©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Λ ड͚औ͓͚ͬͯ͹೚ҙͷλΠϛϯά ͰΩϟϯηϧ͕Ͱ͖Δ
  114. ©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() Ωϟϯηϧͷྫ֎͕ඈΜͰ͘ΔͷͰ Ωϟον
  115. ©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() } }
  116. ©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() } } ϥΠϑαΠΫϧʹ߹Θͤ ͯΩϟϯηϧ
  117. ©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() }
  118. ©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() }
  119. ©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() }
  120. ©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() } ·ͱΊͯΩϟϯηϧ
  121. ©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) } }
  122. ©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() }
  123. ©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จͱҰॹɻ͜Ε͸ί ϧʔνϯͰ͸ͳ͍ɻ
  124. ©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͔Β ൈ͚Δ
  125. ©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Λൈ͚Δ
  126. ©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() } ϦτϥΠ͕શࣦͯഊ͔ͨ͠Ϧτϥ Π͢΂͖Ͱͳ͍Τϥʔ͕ൃੜ৔߹
  127. ©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<ResponseBody> @DELETE(“/api/v1/articles/{id}/like") fun likeCancel(@Path("id") id: Long): Deferred<ResponseBody> @POST(“/api/v1/users/sign_up”) fun signUp(@Body params: SignUpParameter): Deferred<SignInResponseEntity> @POST("/api/v1/users/sign_in") fun signIn(@Body params: SignInParameter): Deferred<SignInResponseEntity>
  128. ©2018 Lang-8 Inc. ALL Rights Reserved. &WFOU#VTͱ$IBOOFM class EventBus {

    private val channel = BroadcastChannel<Any>(1) fun send(event: Any, context: CoroutineContext = DefaultDispatcher) { launch(context) { channel.send(event) } } fun subscribe(): ReceiveChannel<Any> = channel.openSubscription() inline fun <reified T> subscribeToEvent() = subscribe().filter { it is T }.map { it as T } } IUUQTHJTUHJUIVCDPNTWFOKBDPCTBCEEBCDEB
  129. ©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<LogoutEvent>().consumeEach { // logged out } } } override fun onPause() { rootJob.cancel() super.onPause() } //… bus.send(LogoutEvent)
  130. ©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΋͋ΔΑ
  131. ©2018 Lang-8 Inc. ALL Rights Reserved. w ܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖ͯศར w ,PUMJO͸ίϧʔνϯΛεςʔτϚγϯʹม׵࣮ͯ͠ݱ͠

    ͍ͯΔ w BTZODBXBJUͳͲͷ֤ػೳ͸,PUMJOͷެࣜϥΠϒϥϦ ͕ఏڙ͍ͯ͠Δ w MBVODIؔ਺ͱBTZODؔ਺Ͱେ఍଍ΓΔ w ඞཁʹԠͯࣗ͡෼Ͱίϧʔνϯͷ࢓૊ΈΛ࣮૷Ͱ͖Δɻ͢ ͰʹαʔυύʔςΟϥΠϒϥϦ͕͍͔ͭ͋͘Δ ,PUMJOίϧʔνϯ·ͱΊ
  132. ©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL

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

    Rights Reserved. ͋Γ͕ͱ͏͍͟͝·ͨ͠