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

Androidと非同期処理 とCoroutine1.0.0

800912a73ce4e879003b6c89cf80cfeb?s=47 yagi
December 18, 2018

Androidと非同期処理 とCoroutine1.0.0

- Kotlin1.3とCoroutine1.0.0とJetpack
- なぜAndroidでコルーチンを使いたいか
- Androidでコルーチンを使う準備
- 並行処理を書く(直列、並列、スコープと例外)
- おまけ1: Rx with Coroutine
- おまけ2: Jetpack with Coroutine

800912a73ce4e879003b6c89cf80cfeb?s=128

yagi

December 18, 2018
Tweet

Transcript

  1. ©2018 Lang-8 Inc. ALL Rights Reserved. "QQ%PKP 5PTIJIJSP:BHJ "OESPJEͱඇಉظॲཧ ͱ$PSPVUJOF

  2. ©2018 Lang-8 Inc. ALL Rights Reserved. wീ໦ढ़޿ w!TZTZBHJ w$50PG w"OESPJE&OHJOFFS

    "CPVU.F
  3. ©2018 Lang-8 Inc. ALL Rights Reserved. "OESPJE%FW4VNNJUb IUUQTXXXZPVUVCFDPNXBUDI W&0KR0*8,R.

  4. ©2018 Lang-8 Inc. ALL Rights Reserved. w ,PUMJOͱ$PSPVUJOFͱ+FUQBDL w ͳͥ"OESPJEͰίϧʔνϯΛ࢖͍͍͔ͨ

    w "OESPJEͰίϧʔνϯΛ࢖͏४උ w ฒߦॲཧΛॻ͘ ௚ྻɺฒྻɺείʔϓͱྫ֎  w ͓·͚3YXJUI$PSPVUJOF w ͓·͚+FUQBDLXJUI$PSPVUJOF ࠓ೔࿩͢͜ͱ
  5. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJOͱ $PSPVUJOFͱ +FUQBDL

  6. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO

  7. ©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO w$PSPVUJOF 4UBCMF 

    w,PUMJO/BUJWF#FUB w,UPS#FUB wΠϯϥΠϯΫϥεɺίϯτϥΫτɺXIFO ࣜͷαϒδΣΫτFUDʜ
  8. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF w$PSPVUJOF4DPQFΛಋೖ͠طଘͷॻ͖ํ͕ EFQSFDBUFEʹ d

     w%JTQBUDIFSTΛಋೖ d  wείʔϓͷྫ֎ͷ࢓༷͕มΘͬͨ d  wFYQFSJNFOUBMQBDLBHFΛഇࢭ d IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTSFMFBTFT
  9. ©2018 Lang-8 Inc. ALL Rights Reserved. +FUQBDLXJUI$PSPVUJOF IUUQTEFWFMPQFSBOESPJEDPNKFUQBDLEPDTSFMFBTFOPUFT

  10. ©2018 Lang-8 Inc. ALL Rights Reserved. +FUQBDLXJUI$PSPVUJOF w8PSL.BOBHFSʹ$PSPVUJOF8PSLFSΛ௥Ճ w3PPNͰTVTQFOEؔ਺Λαϙʔτ w7JFX.PEFMʹWJFX.PEFM4DPQFΛ௥Ճ༧ఆ

  11. ©2018 Lang-8 Inc. ALL Rights Reserved. +FUQBDLXJUI$PSPVUJOF w8PSL.BOBHFSʹ$PSPVUJOF8PSLFSΛ௥Ճ w3PPNͰTVTQFOEؔ਺Λαϙʔτ w7JFX.PEFMʹWJFX.PEFM4DPQFΛ௥Ճ༧ఆ

    "OESPJEͰίϧʔνϯΛศརʹ࢖͏؀ڥ͕ ੔͑ΒΕ͍͍ͯͬͯΔɻ
  12. ©2018 Lang-8 Inc. ALL Rights Reserved. "OESPJE%FW4VNNJUb IUUQTXXXZPVUVCFDPNXBUDI W&0KR0*8,R.

  13. ©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥ"OESPJEͰίϧʔνϯ Λ࢖͍͍ͨͷ͔

  14. ©2018 Lang-8 Inc. ALL Rights Reserved. w7JFXͷJOqBUF NFBTVSF MBZPVU Ϩϯ

    μϦϯάɺλονΠϕϯτͳͲදࣔʹؔΘΔ ॲཧΛ͢Δ w)[ͳΒϑϨʔϜNT͔͠ͳ͍ ϑϨʔ Ϝམͪ͸ମݧΛଛͶΔ  w௕࣌ؒͷϒϩοΩϯά͸"/3ʹͳΔ "OESPJEͱ6*εϨου
  15. ©2018 Lang-8 Inc. ALL Rights Reserved. w$36% w%BUBCBTFɺ'JMF*0 w4ZOD w/FUXPSL*0

    w$PNQVUF wෳࡶͳܭࢉ ΄ͱΜͲͷΞϓϦ͕ߦ͏ॏ͍ͨॲཧ
  16. ©2018 Lang-8 Inc. ALL Rights Reserved. w"TZOD5BTL w&YFDVUPST ͔ͳΓ௿ϨΠϠʔ 

    w-PBEFST ݱࡏ͸ഇࢭ  w'VUVSF NJO4EL7FSTJPOҎ্  w-JCSBSJFT 3Y+BWBɺ࣮࣭σϑΝΫτελϯμʔυ "OESPJEͷඇಉظॲཧ
  17. ©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥίϧʔνϯΛ࢖͍͍͔ͨ

  18. ©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥίϧʔνϯΛ࢖͍͍͔ͨ wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ

  19. ©2018 Lang-8 Inc. ALL Rights Reserved. εϨουͷ࣮ߦΠϝʔδ 5ISFBE1PPM 8PSLFS 8PSLFS

    8PSLFS 8PSLFS εϨου .#d.#΄ ͲϝϞϦΛ࢖͏
  20. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯͷ࣮ߦΠϝʔδ 5ISFBE1PPM 8PSLFS 8PSLFS

    8PSLFS 8PSLFS $PSPVUJOF ʜ ʜ εϨου .#d.#΄ ͲϝϞϦΛ࢖͏ ͭͷίϧʔν ϯͰ,#΄Ͳɻ εϨουͰෳ ਺࣮ߦ͢Δɻ
  21. ©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥίϧʔνϯΛ࢖͍͍͔ͨ wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ w εϨουd.#ʹରͯ͠ɺίϧʔνϯ͸,#ఔ౓

    w؆୯ʹ࢖͑Δ ಡΈॻ͖ͷ͠΍͢͞
  22. ©2018 Lang-8 Inc. ALL Rights Reserved. wҰ࣌ఀࢭՄೳͳܭࢉΠϯελϯε wεϨουͱಉ͡Α͏ʹϥΠϑαΠ ΫϧΛ࣋ͭ wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қ

    ʹهड़Ͱ͖Δ ίϧʔνϯͱ͸
  23. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

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

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } "1*ʹϦΫΤετͯ݁͠ՌΛฦؔ͢਺
  25. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } ׬ྃ࣌ͷίʔϧόοΫΛҾ਺ʹऔΔ
  26. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } ίʔϧόοΫʹଓ͖ͷॲཧΛॻ͘
  27. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ͸ fun getProfile(id: Int,

    f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } ίʔϧόοΫʹଓ͖ͷॲཧΛॻ͘ ܧଓঢ়گΛ࣋ͭϓϩάϥϜ
  28. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜΛ༰қʹهड़͢Δ suspend fun getProfile(id:

    Int): Profile suspend fun loadProfile(id: Int) { val profile = getProfile(token) showProfile(profile) }
  29. ©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜΛ༰қʹهड़͢Δ suspend fun getProfile(id:

    Int): Profile suspend fun loadProfile(id: Int) { val profile = getProfile(token) showProfile(profile) } ϫʔΧʔεϨουͰಈ࡞ 6*εϨουͰಈ࡞
  30. ©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥίϧʔνϯΛ࢖͍͍͔ͨ wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ w εϨουd.#ʹରͯ͠ɺίϧʔνϯ͸,#ఔ౓

    w؆୯ʹ࢖͑Δ ಡΈॻ͖ͷ͠΍͢͞  wܰྔͳϥΠϒϥϦ
  31. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯ͸ϥΠϒϥϦͰఏڙ͞Ε͍ͯΔ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT

  32. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯ͸ϥΠϒϥϦͰఏڙ͞Ε͍ͯΔ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT dependencies {

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1' implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.0.1' }
  33. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯ͸ϥΠϒϥϦͰఏڙ͞Ε͍ͯΔ dependencies { implementation

    'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1' implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.0.1' } wKBS,# wBQL,# wPQUJNJ[F࠷খ,#
  34. ©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥίϧʔνϯΛ࢖͍͍͔ͨ wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ w εϨουd.#ʹରͯ͠ɺίϧʔνϯ͸,#ఔ౓

    w؆୯ʹ࢖͑Δ ಡΈॻ͖ͷ͠΍͢͞  wܰྔͳϥΠϒϥϦ w KBS,# BQL,# PQUJNJ[F࠷খ,#
  35. ©2018 Lang-8 Inc. ALL Rights Reserved. "OESPJEͰίϧʔνϯΛ࢖͏४උ

  36. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ༻ҙ͢Δ

  37. ©2018 Lang-8 Inc. ALL Rights Reserved. w$PSPVUJOF4DPQF͸ίϧʔνϯͷ࣮ߦ؀ڥ $PSPVUJOF4DPQFΛ༻ҙ͢Δ

  38. ©2018 Lang-8 Inc. ALL Rights Reserved. w$PSPVUJOF4DPQF͸ίϧʔνϯͷ࣮ߦ؀ڥ wىಈͨ͠ίϧʔνϯ͸$PSPVUJOF4DPQFͷ ࢠʹͳΔ $PSPVUJOF4DPQFΛ༻ҙ͢Δ

  39. ©2018 Lang-8 Inc. ALL Rights Reserved. w$PSPVUJOF4DPQF͸ίϧʔνϯͷ࣮ߦ؀ڥ wىಈͨ͠ίϧʔνϯ͸$PSPVUJOF4DPQFͷ ࢠʹͳΔ w$PSPVUJOF4DPQF͕ऴྃ͢Δͱࢠίϧʔν

    ϯ΋ऴྃ͢Δ $PSPVUJOF4DPQFΛ༻ҙ͢Δ
  40. ©2018 Lang-8 Inc. ALL Rights Reserved. w$PSPVUJOF4DPQF͸ίϧʔνϯͷ࣮ߦ؀ڥ wىಈͨ͠ίϧʔνϯ͸$PSPVUJOF4DPQFͷ ࢠʹͳΔ w$PSPVUJOF4DPQF͕ऴྃ͢Δͱࢠίϧʔν

    ϯ΋ऴྃ͢Δ wϥΠϑαΠΫϧʹ߹Θͤͯ$PSPVUJOF4DPQF Λ༻ҙ͢Δ $PSPVUJOF4DPQFΛ༻ҙ͢Δ
  41. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job
  42. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job public interface CoroutineScope { public val coroutineContext: CoroutineContext }
  43. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job
  44. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job
  45. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job ࣮ߦ؀ڥΛઃఆ͢Δ
  46. ©2018 Lang-8 Inc. ALL Rights Reserved. %JTQBUDIFS͸࣮ߦ؀ڥΛ࢘Δ wίϧʔνϯΛ࣮ߦ͢ΔεϨου΍࣮ߦεέδϡʔ ϧΛ؅ཧ͢Δ w%JTQBUDIFSTΦϒδΣΫτʹ·ͱ·͍ͬͯΔ

    w༻్ʹ߹Θͤͯ੾Γସ͑Δ
  47. ©2018 Lang-8 Inc. ALL Rights Reserved. %JTQBUDIFST.BJO wϝΠϯεϨουͰ࣮ߦ͢Δ w"OESPJEͷ৔߹͸6*εϨου )BOEMFS

    wLPUMJOYDPSPVUJOFTBOESPJE͕ඞཁ
  48. ©2018 Lang-8 Inc. ALL Rights Reserved. %JTQBUDIFST%FGBVMU wܭࢉ༻εϨουϓʔϧ w$16ίΞ਺෼֬อ͞Ε͍ͯΔ wԿ΋ࢦఆ͠ͳ͍৔߹࢖ΘΕΔ

  49. ©2018 Lang-8 Inc. ALL Rights Reserved. %JTQBUDIFST*0 w*0ʹ࠷దԽͨ͠εϨουϓʔϧ w࠷௿ฒߦ w%FGBVMUͱεϨουϓʔϧͱڞ༗͍ͯ͠

    ΔͷͰߴ଎ Β͍͠ˣ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTCMPCNBTUFSDPSFLPUMJOYDPSPVUJOFTDPSFTSDTDIFEVMJOH$PSPVUJOF4DIFEVMFSLU
  50. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job
  51. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job ࢠίϧʔνϯͷϋϯυϥ
  52. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } }
  53. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } } ίϧʔνϯΛىಈ
  54. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } } NTఀࢭͯ͠࠶։͢Δ
  55. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } }
  56. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ override fun onPause()

    { job.cancelChildren() super.onPause() }
  57. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ override fun onPause()

    { job.cancelChildren() super.onPause() } ࢠίϧʔνϯΛ͢΂ͯ Ωϟϯηϧ ऴྃ ͢Δ
  58. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ override fun onPause()

    { job.cancelChildren() super.onPause() }
  59. ©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮૷͢Δ class MainActivity :

    AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch {/*…*/} } override fun onPause() { job.cancelChildren() super.onPause() } }
  60. ©2018 Lang-8 Inc. ALL Rights Reserved. 7JFX.PEFMͱ$PSPVUJOF4DPQF WJFXNPEFMLUYIUUQSBOESPJEDPN w֦ுϓϩύςΟʮWJFX.PEFM4DPQFʯ͕௥Ճ༧ఆ

  61. ©2018 Lang-8 Inc. ALL Rights Reserved. "OESPJEͰίϧʔνϯΛ࢖͏४උ wίϧʔνϯΛ࣮ߦ͢Δʹ͸ $PSPVUJOF4DPQF͕ඞཁ wϥΠϑαΠΫϧΛ࣋ͭίϯϙʔωϯτͰ

    $PSPVUJOF4DPQFΛ࣮૷͢Δ w࣮ߦ؀ڥͷઃఆͱΩϟϯηϧॲཧΛ͢Δ
  62. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦॲཧΛॻ͘ ௚ྻɺฒྻɺείʔϓͱྫ֎

  63. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔͱ TVTQFOEؔ਺

  64. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔͱ͸ wίϧʔνϯΛ։࢝͢Δؔ਺

  65. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔͱ͸ wίϧʔνϯΛ։࢝͢Δؔ਺ wMBVODIؔ਺໭Γ஋Λ࣋ͨͳ͍ wBTZODؔ਺໭Γ஋Λ࣋ͭ

  66. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔͱ͸ wίϧʔνϯΛ։࢝͢Δؔ਺ wMBVODIؔ਺໭Γ஋Λ࣋ͨͳ͍ wBTZODؔ਺໭Γ஋Λ࣋ͭ

    w$PSPVUJOF4DPQFͷ֦ுؔ਺
  67. ©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔͱ͸ wίϧʔνϯΛ։࢝͢Δؔ਺ wMBVODIؔ਺໭Γ஋Λ࣋ͨͳ͍ wBTZODؔ਺໭Γ஋Λ࣋ͭ

    w$PSPVUJOF4DPQFͷ֦ுؔ਺ w౉ͨ͠ϥϜμࣜΛίϧʔνϯͱ͠ ͯىಈ͢Δ
  68. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ͸ wεϨουΛϒϩοΩϯά͢Δ͜ͱͳ͘ॲཧΛఀ ࢭͰ͖Δؔ਺

  69. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ͸ wεϨουΛϒϩοΩϯά͢Δ͜ͱͳ͘ॲཧΛఀ ࢭͰ͖Δؔ਺ wίʔϧόοΫͷΑ͏ʹ͋ͱͰఀࢭҐஔ͔Β࠶։

    Ͱ͖Δ
  70. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ͸ wεϨουΛϒϩοΩϯά͢Δ͜ͱͳ͘ॲཧΛఀ ࢭͰ͖Δؔ਺ wίʔϧόοΫͷΑ͏ʹ͋ͱͰఀࢭҐஔ͔Β࠶։

    Ͱ͖Δ wTVTQFOEؔ਺͸ଞͷTVTQFOEؔ਺͔ΒͷΈ ݺͼग़͕͠Ͱ͖Δ
  71. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ͸ wεϨουΛϒϩοΩϯά͢Δ͜ͱͳ͘ॲཧΛఀ ࢭͰ͖Δؔ਺ wίʔϧόοΫͷΑ͏ʹ͋ͱͰఀࢭҐஔ͔Β࠶։

    Ͱ͖Δ wTVTQFOEؔ਺͸ଞͷTVTQFOEؔ਺͔ΒͷΈ ݺͼग़͕͠Ͱ͖Δ wίϧʔνϯϏϧμʔʹΘͨ͢ϥϜμࣜ͸ TVTQFOEϥϜμʹͳ͍ͬͯΔɻ
  72. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } }
  73. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ͜͜ͰॲཧΛதஅ͠ɺ࠶։࣌ʹ1SPpMFΛฦؔ͢਺
  74. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ίϧʔνϯͷ࣮ߦ؀ڥΛ੾Γସ͑ΔTVTVQFOEؔ਺
  75. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ίϧʔνϯͷ࣮ߦ؀ڥΛ੾Γସ͑ΔTVTVQFOEؔ਺ ͜͜Ͱ͸ϫʔΧʔεϨουͰಈ͘ઃఆΛ͍ͯ͠Δ
  76. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } "1*ϦΫΤετɻϒϩοΩϯά͢Δॲཧ
  77. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ίϧʔνϯΛىಈ %JTQBUDIFST.BJO KPCͱ͢Δ
  78. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } }
  79. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ
  80. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞
  81. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠୅ೖ
  82. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠୅ೖ 6*εϨουͰಈ࡞
  83. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠୅ೖ 6*εϨουͰಈ࡞ ᶅ7JFXʹ൓ө
  84. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠୅ೖ 6*εϨουͰಈ࡞ ᶅ7JFXʹ൓ө ྫ֎࣌͸ͬͪ͜
  85. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠୅ೖ 6*εϨουͰಈ࡞ ᶅ7JFXʹ൓ө ྫ֎࣌͸ͬͪ͜ Ωϟϯηϧ είʔϓ͕ऴྃͨ͠ ͸ͬͪ͜
  86. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } }
  87. ©2018 Lang-8 Inc. ALL Rights Reserved. ௚ྻʹॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) { requestTicketCount(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) val ticketCount = getTicketCount(id) view.showProfile(profile, ticketCount) } catch (e: Exception) { view.showError(e) } } }
  88. ©2018 Lang-8 Inc. ALL Rights Reserved. ௚ྻʹॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) { requestTicketCount(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) val ticketCount = getTicketCount(id) view.showProfile(profile, ticketCount) } catch (e: Exception) { view.showError(e) } } }
  89. ©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ wXJUI$POUFYUؔ਺Λ࢖ͬͯίϧʔνϯͷ࣮ߦ ؀ڥΛ੾Γସ͑Δ w௚ྻͰ࣮ߦ͢Δ৔߹͸୯७ʹฒ΂Δ͚ͩͰྑ͍

    wΤϥʔͱΩϟϯηϧ͸USZDBUDIͰॲཧ͢Δ wඇಉظॲཧͷ΄ͱΜͲ͸͜ΕͰରԠͰ͖Δ͸ͣ
  90. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } }
  91. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } }
  92. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } DPSPVUJOF4DPQFؔ਺Λ࢖ͬͯ ࢠείʔϓΛ։࢝͢Δ
  93. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } BTZODؔ਺ͰίϧʔνϯΛෳ਺ىಈ͢Δ
  94. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } ଴ͪ߹ΘͤΔ
  95. ©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQFؔ਺͕ͳ͔ͬͨΒ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } catch (e: Exception) { view.showError(e) } } }
  96. ©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQFؔ਺͕ͳ͔ͬͨΒ suspend fun getProfile(id:

    Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } catch (e: Exception) { view.showError(e) } } }
  97. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑͷߏ଄Խ IUUQTNFEJVNDPN!FMJ[BSPWTUSVDUVSFEDPODVSSFODZEBB

  98. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑͷߏ଄Խ IUUQTWPSQVTPSHCMPHOPUFTPOTUSVDUVSFEDPODVSSFODZPSHPTUBUFNFOUDPOTJEFSFEIBSNGVM

  99. ©2018 Lang-8 Inc. ALL Rights Reserved. wߏ଄Խϓϩάϥϛϯάͷฒߦੑ൛ wฒߦॲཧʹ͓͚ΔHPUPͷΑ͏ͳ ΋ͷΛͳٞ͘͢࿦ w$PSPVUJOF4DPQF͸͜ͷٞ࿦

    Λجʹಋೖ͞Εͨ ฒߦੑͷߏ଄Խͬͯʁ
  100. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑʹ͓͚ΔHPUP void save(int data,

    File file) { try (FileOutputStream out = new FileOutputStream(file);) { new Thread() { @Override public void run() { try { out.write(data); } catch (IOException e) { // error } } }.start(); } catch (IOException e) { // error } }
  101. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑʹ͓͚ΔHPUP void save(int data,

    File file) { try (FileOutputStream out = new FileOutputStream(file);) { new Thread() { @Override public void run() { try { out.write(data); } catch (IOException e) { // error } } }.start(); } catch (IOException e) { // error } } USZXJUISFTPVSDFTͰϑΝΠϧΛ։͘
  102. ©2018 Lang-8 Inc. ALL Rights Reserved. void save(int data, File

    file) { try (FileOutputStream out = new FileOutputStream(file);) { new Thread() { @Override public void run() { try { out.write(data); } catch (IOException e) { // error } } }.start(); } catch (IOException e) { // error } } ฒߦੑʹ͓͚ΔHPUP USZXJUISFTPVSDFTͷதͰεϨου Λىಈͯ͠PVUʹ৮ͬͯ΋ඞͣDMPTFࡁ ΈͳͷͰΤϥʔʹͳΔ
  103. ©2018 Lang-8 Inc. ALL Rights Reserved. wฒߦ෼ذͷ։࢝ͱऴྃΛͻͱ·ͱ·Γͷ୯Ґ ʹͯ͠ɺͦΕΒΛ֊૚ߏ଄ʹ͢Δ ฒߦੑʹ͓͚ΔHPUPΛ๷͙ΞΠσΞ ฒߦ෼ذͷ·ͱ·Γ

    $PSPVUJOF4DPQF
  104. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑͷߏ଄ԽͷΠϝʔδ

  105. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑΛߏ଄Խ͢Δ fun showProfile(id: Int)

    { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } }
  106. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑΛߏ଄Խ͢Δ fun showProfile(id: Int)

    { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } 1BSFOU4DPQF $IJME4DPQF MBVODI BTZOD BTZOD
  107. ©2018 Lang-8 Inc. ALL Rights Reserved. είʔϓͱྫ֎ 1BSFOU4DPQF $IJME4DPQF MBVODI

    BTZOD BTZOD
  108. ©2018 Lang-8 Inc. ALL Rights Reserved. είʔϓͱྫ֎ 1BSFOU4DPQF $IJME4DPQF MBVODI

    BTZOD BTZOD
  109. ©2018 Lang-8 Inc. ALL Rights Reserved. είʔϓͱྫ֎ 1BSFOU4DPQF $IJME4DPQF MBVODI

    BTZOD BTZOD είʔϓ಺ͷฒߦ෼ذͷ͍ͣΕ͔͕ΤϥʔʹͳΔͱ είʔϓશମ͕Τϥʔ Ωϟϯηϧ ʹͳΔ
  110. ©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQFؔ਺ fun showProfile(id: Int)

    { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } UISPX&YDFQUJPO
  111. ©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQFؔ਺ fun showProfile(id: Int)

    { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } ࢠείʔϓ͕ऴྃ͠ྫ֎͕ඈΜͰ͘Δ
  112. ©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQFؔ਺͕ͳ͔ͬͨΒ fun showProfile(id: Int)

    { launch { try { val profile = async { requestProfile(id) } val tickets = async { requestTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } catch (e: Exception) { view.showError(e) } } }
  113. ©2018 Lang-8 Inc. ALL Rights Reserved. 1BSFOU4DPQF MBVODI BTZOD BTZOD

    DPSPVUJOF4DPQFؔ਺͕ͳ͔ͬͨΒ
  114. ©2018 Lang-8 Inc. ALL Rights Reserved. 1BSFOU4DPQF MBVODI BTZOD BTZOD

    DPSPVUJOF4DPQFؔ਺͕ͳ͔ͬͨΒ
  115. ©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻॲཧΛॻ͘ wίϧʔνϯͷதͰίϧʔνϯ ෼ذ Λىಈ͢Δ৔

    ߹͸DPSPVUJOF4DPQFؔ਺Λ࢖͏ είʔϓͷ ֊૚ߏ଄Λߟ͑Δ  wMBVODIBTZODΛωετͯ͠Δ࣌͸஫ҙ w෼ذ͕Τϥʔʹͳͬͯ΋είʔϓΛܧଓͰ͖Δ TVQFSWJTPS4DPQFؔ਺͕͋Δ ࠓճ͸লུ IUUQTLPUMJOHJUIVCJPLPUMJOYDPSPVUJOFTLPUMJOYDPSPVUJOFTDPSFLPUMJOYDPSPVUJOFTTVQFSWJTPSTDPQFIUNM
  116. ©2018 Lang-8 Inc. ALL Rights Reserved. 3YXJUI$PSPVUJOF

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

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

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

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

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

  122. ©2018 Lang-8 Inc. ALL Rights Reserved. 3Y+BWBͱͷ౷߹ wLPUMJOYDPSPVUJOFTSYΛ࢖͏ w4JOHMF .BZCF

    $PNQMFUBCMFͳͲΛ TVTQFOEؔ਺ʹͰ͖Δ wTVTQFOEؔ਺Λ4JOHMF .BZCF  $PNQMFUBCMFͳͲͰ͖Δ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTUSFFNBTUFSSFBDUJWFLPUMJOYDPSPVUJOFTSY
  123. ©2018 Lang-8 Inc. ALL Rights Reserved. 4JOHMFΛTVTQFOEؔ਺ʹ͢Δ interface RemoteService {

    @GET("/profile") fun getProfile(id: Int): Single<Profile> }
  124. ©2018 Lang-8 Inc. ALL Rights Reserved. 4JOHMFΛTVTQFOEؔ਺ʹ͢Δ interface RemoteService {

    @GET("/profile") fun getProfile(id: Int): Single<Profile> } val profile = withContext { service.getProfile(id).await() } 4JOHMFͷ֦ுؔ਺ʹ BXBJU ͕௥Ճ͞Ε͍ͯΔ
  125. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺Λ4JOHMFʹ͢Δ fun getProfile(id: Int):

    Single<Profile> { return rxSingle { Profile(id) } } TVTQFOEϥϜμΛड͚औͬͯ݁ՌΛ 4JOHMFͰฦؔ͢਺
  126. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺Λ4JOHMFʹ͢Δ fun getProfile(id: Int):

    Single<Profile> { return rxSingle { Profile(id) } } TVTQFOEϥϜμΛड͚औͬͯ݁ՌΛ 4JOHMFͰฦؔ͢਺ Τϥʔʹͳͬͯ΋਌είʔϓΛ Ωϟϯηϧ͠ͳ͍
  127. ©2018 Lang-8 Inc. ALL Rights Reserved. 3Y+BWBXJUI$PSPVUJPOF w LPUMJOYDPSPVUJOFTSYͰ૬ޓͷΠϯςάϨʔγϣ ϯ͕Մೳ

    w ྫ͑͹ෳࡶʹ߹੒͕ඞཁͳՕॴͳͲ͸ίϧʔνϯͰϑϥο τʹॻ͍ͯ࠷ޙʹ4JOHMFʹ͢Δͱ͔͕͍͍͔΋ w ରཱ͢Δ΋ͷͰ͸ͳ͍ͷͰ͍͍ײ͡ʹ࢖͏ͷ͕ྑ͍ ͓͢ ͢Ί͸4JOHMF .BZCF $PNQMFUBCMFͷஔ͖׵͑
  128. ©2018 Lang-8 Inc. ALL Rights Reserved. +FUQBDLXJUI$PSPVUJOF

  129. ©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱίϧʔνϯ wBMQIBͰ$PSPVUJOF8PSLFSΛ௥Ճ

  130. ©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,

    param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } }
  131. ©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,

    param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } EP8PSLؔ਺͕TVTQFOEؔ਺ʹͳΔ
  132. ©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,

    param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } ΦʔόʔϥΠυͰ͖Δɻ σϑΥϧτ͸%JTQBUDIFST%FGBVMU
  133. ©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,

    param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } ໭Γ஋͕8PSLFSͱ͸ҟͳΔɻ PVUQVU%BUBΛ໌ࣔతʹ౉͢ඞཁ͕͋Δ
  134. ©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,

    param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } ໭Γ஋͕8PSLFSͱ͸ҟͳΔɻ PVUQVU%BUBΛ໌ࣔతʹ౉͢ඞཁ͕͋Δ public Payload(@NonNull Result result, @NonNull Data output) { mResult = result; mOutput = output; }
  135. ©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱίϧʔνϯ w BMQIBͰ%"0ͰTVTQFOEؔ਺Λαϙʔτ

  136. ©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ਺ @Dao interface UserDao

    { @Insert suspend fun insert(user: User) @Update suspend fun update(user: User) @Query("SELECT * FROM user") suspend fun getAllUsers(): List<User> @Delete suspend fun delete(user: User) }
  137. ©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ਺ val job =

    Job() val scope = CoroutineScope(Dispatchers.Main + job) scope.launch { try { val dao = db.userDao() dao.insert(User(firstName = "Jack", lastName = "Jill")) // do something } catch (e: java.lang.Exception) { // insert error } }
  138. ©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ਺ val job =

    Job() val scope = CoroutineScope(Dispatchers.Main + job) scope.launch { try { val dao = db.userDao() dao.insert(User(firstName = "Jack", lastName = "Jill")) // do something } catch (e: java.lang.Exception) { // insert error } } ಺෦ͰXJUI$POUFYUؔ਺͕ݺͼग़͞ΕΔ
  139. ©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ਺ val job =

    Job() val scope = CoroutineScope(Dispatchers.Main + job) scope.launch { try { val dao = db.userDao() dao.insert(User(firstName = "Jack", lastName = "Jill")) // do something } catch (e: java.lang.Exception) { // insert error } } ಺෦ͰXJUI$POUFYUؔ਺͕ݺͼग़͞ΕΔ 3PPNͷ2VFSZ&YFDVUPSΛ %JTQBUDIFSʹ͍ͯ͠Δ
  140. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ2VFSZ&YFDVUPS val db =

    Room.databaseBuilder( applicationContext, AppDatabase::class.java, "database-name" ).apply { val dispatcher = Dispatchers.IO if (dispatcher is ExecutorCoroutineDispatcher) { setQueryExecutor(dispatcher.executor) } }.build()
  141. ©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔ਺ͱ2VFSZ&YFDVUPS val db =

    Room.databaseBuilder( applicationContext, AppDatabase::class.java, "database-name" ).apply { val dispatcher = Dispatchers.IO if (dispatcher is ExecutorCoroutineDispatcher) { setQueryExecutor(dispatcher.executor) } }.build() 3PPN#VJMEFSͰ&YFDVUPSΛઃఆͰ͖Δ
  142. ©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊ w ίϧʔνϯ͸লϝϞϦɺॻ͖΍͍͢ ಡΈ΍͍͢

    ɺϥΠϒϥϦͱ ͯ͠΋ܰྔ w $PSPVUJOF4DPQFͱϥΠϑαΠΫϧΛ࣋ͭίϯϙʔωϯτΛ૊ Έ߹Θͤͯ࢖͏ w ୯७ͳඇಉظॲཧ͸XJUI$POUFYUؔ਺Ͱେ఍͸ΧόʔͰ͖Δ w ฒྻͰίϧʔνϯΛىಈ͢Δ৔߹͸είʔϓͷ֊૚Λҙࣝ͢Δඞ ཁ͕͋Δɻ w 3Yͱ͸ରཱ͠ͳ͍ɻҰ෦ͷॲཧ͕ஔ͖׵͕͑Ͱ͖Δ͔΋ w +FUQBDLͰ΋ଓʑαϙʔτ͕ਐΜͰಋೖ͠΍͘͢ͳ͖͍ͬͯͯΔ
  143. ©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL

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