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

Androidと非同期処理 とCoroutine1.0.0

yagi
December 18, 2018

Androidと非同期処理 とCoroutine1.0.0

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

yagi

December 18, 2018
Tweet

More Decks by yagi

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w ,PUMJOͱ$PSPVUJOFͱ+FUQBDL
    w ͳͥ"OESPJEͰίϧʔνϯΛ࢖͍͍͔ͨ
    w "OESPJEͰίϧʔνϯΛ࢖͏४උ
    w ฒߦॲཧΛॻ͘ ௚ྻɺฒྻɺείʔϓͱྫ֎

    w ͓·͚3YXJUI$PSPVUJOF
    w ͓·͚+FUQBDLXJUI$PSPVUJOF
    ࠓ೔࿩͢͜ͱ

    View full-size slide

  5. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ,PUMJOͱ
    $PSPVUJOFͱ
    +FUQBDL

    View full-size slide

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

    View full-size slide

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

    w,PUMJO/BUJWF#FUB
    w,UPS#FUB
    wΠϯϥΠϯΫϥεɺίϯτϥΫτɺXIFO
    ࣜͷαϒδΣΫτFUDʜ

    View full-size slide

  8. ©2018 Lang-8 Inc. ALL Rights Reserved.
    $PSPVUJOF
    w$PSPVUJOF4DPQFΛಋೖ͠طଘͷॻ͖ํ͕
    EFQSFDBUFEʹ d

    w%JTQBUDIFSTΛಋೖ d

    wείʔϓͷྫ֎ͷ࢓༷͕มΘͬͨ d

    wFYQFSJNFOUBMQBDLBHFΛഇࢭ d

    IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTSFMFBTFT

    View full-size slide

  9. ©2018 Lang-8 Inc. ALL Rights Reserved.
    +FUQBDLXJUI$PSPVUJOF
    IUUQTEFWFMPQFSBOESPJEDPNKFUQBDLEPDTSFMFBTFOPUFT

    View full-size slide

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

    View full-size slide

  11. ©2018 Lang-8 Inc. ALL Rights Reserved.
    +FUQBDLXJUI$PSPVUJOF
    w8PSL.BOBHFSʹ$PSPVUJOF8PSLFSΛ௥Ճ
    w3PPNͰTVTQFOEؔ਺Λαϙʔτ
    w7JFX.PEFMʹWJFX.PEFM4DPQFΛ௥Ճ༧ఆ
    "OESPJEͰίϧʔνϯΛศརʹ࢖͏؀ڥ͕
    ੔͑ΒΕ͍͍ͯͬͯΔɻ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  14. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w7JFXͷJOqBUF NFBTVSF MBZPVU Ϩϯ
    μϦϯάɺλονΠϕϯτͳͲදࣔʹؔΘΔ
    ॲཧΛ͢Δ
    w)[ͳΒϑϨʔϜNT͔͠ͳ͍ ϑϨʔ
    Ϝམͪ͸ମݧΛଛͶΔ

    w௕࣌ؒͷϒϩοΩϯά͸"/3ʹͳΔ
    "OESPJEͱ6*εϨου

    View full-size slide

  15. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w$36%
    w%BUBCBTFɺ'JMF*0
    w4ZOD
    w/FUXPSL*0
    w$PNQVUF
    wෳࡶͳܭࢉ
    ΄ͱΜͲͷΞϓϦ͕ߦ͏ॏ͍ͨॲཧ

    View full-size slide

  16. ©2018 Lang-8 Inc. ALL Rights Reserved.
    w"TZOD5BTL
    w&YFDVUPST ͔ͳΓ௿ϨΠϠʔ

    w-PBEFST ݱࡏ͸ഇࢭ

    w'VUVSF NJO4EL7FSTJPOҎ্

    w-JCSBSJFT 3Y+BWBɺ࣮࣭σϑΝΫτελϯμʔυ

    "OESPJEͷඇಉظॲཧ

    View full-size slide

  17. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ͳͥίϧʔνϯΛ࢖͍͍͔ͨ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ίϧʔνϯͷ࣮ߦΠϝʔδ
    5ISFBE1PPM
    8PSLFS
    8PSLFS
    8PSLFS
    8PSLFS
    $PSPVUJOF ʜ
    ʜ
    εϨου
    .#d.#΄
    ͲϝϞϦΛ࢖͏
    ͭͷίϧʔν
    ϯͰ,#΄Ͳɻ
    εϨουͰෳ
    ਺࣮ߦ͢Δɻ

    View full-size slide

  21. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ͳͥίϧʔνϯΛ࢖͍͍͔ͨ
    wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ
    w εϨουd.#ʹରͯ͠ɺίϧʔνϯ͸,#ఔ౓
    w؆୯ʹ࢖͑Δ ಡΈॻ͖ͷ͠΍͢͞

    View full-size slide

  22. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wҰ࣌ఀࢭՄೳͳܭࢉΠϯελϯε
    wεϨουͱಉ͡Α͏ʹϥΠϑαΠ
    ΫϧΛ࣋ͭ
    wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қ
    ʹهड़Ͱ͖Δ
    ίϧʔνϯͱ͸

    View full-size slide

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

    View full-size slide

  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*ʹϦΫΤετͯ݁͠ՌΛฦؔ͢਺

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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*εϨουͰಈ࡞

    View full-size slide

  30. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ͳͥίϧʔνϯΛ࢖͍͍͔ͨ
    wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ
    w εϨουd.#ʹରͯ͠ɺίϧʔνϯ͸,#ఔ౓
    w؆୯ʹ࢖͑Δ ಡΈॻ͖ͷ͠΍͢͞

    wܰྔͳϥΠϒϥϦ

    View full-size slide

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

    View full-size slide

  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'
    }

    View full-size slide

  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࠷খ,#

    View full-size slide

  34. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ͳͥίϧʔνϯΛ࢖͍͍͔ͨ
    wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ
    w εϨουd.#ʹରͯ͠ɺίϧʔνϯ͸,#ఔ౓
    w؆୯ʹ࢖͑Δ ಡΈॻ͖ͷ͠΍͢͞

    wܰྔͳϥΠϒϥϦ
    w KBS,# BQL,# PQUJNJ[F࠷খ,#

    View full-size slide

  35. ©2018 Lang-8 Inc. ALL Rights Reserved.
    "OESPJEͰίϧʔνϯΛ࢖͏४උ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  46. ©2018 Lang-8 Inc. ALL Rights Reserved.
    %JTQBUDIFS͸࣮ߦ؀ڥΛ࢘Δ
    wίϧʔνϯΛ࣮ߦ͢ΔεϨου΍࣮ߦεέδϡʔ
    ϧΛ؅ཧ͢Δ
    w%JTQBUDIFSTΦϒδΣΫτʹ·ͱ·͍ͬͯΔ
    w༻్ʹ߹Θͤͯ੾Γସ͑Δ

    View full-size slide

  47. ©2018 Lang-8 Inc. ALL Rights Reserved.
    %JTQBUDIFST.BJO
    wϝΠϯεϨουͰ࣮ߦ͢Δ
    w"OESPJEͷ৔߹͸6*εϨου)BOEMFS
    wLPUMJOYDPSPVUJOFTBOESPJE͕ඞཁ

    View full-size slide

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

    View full-size slide

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

    IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTCMPCNBTUFSDPSFLPUMJOYDPSPVUJOFTDPSFTSDTDIFEVMJOH$PSPVUJOF4DIFEVMFSLU

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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++}")
    }
    }
    }
    ίϧʔνϯΛىಈ

    View full-size slide

  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ఀࢭͯ͠࠶։͢Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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()
    }
    }

    View full-size slide

  60. ©2018 Lang-8 Inc. ALL Rights Reserved.
    7JFX.PEFMͱ$PSPVUJOF4DPQF
    WJFXNPEFMLUYIUUQSBOESPJEDPN
    w֦ுϓϩύςΟʮWJFX.PEFM4DPQFʯ͕௥Ճ༧ఆ

    View full-size slide

  61. ©2018 Lang-8 Inc. ALL Rights Reserved.
    "OESPJEͰίϧʔνϯΛ࢖͏४උ
    wίϧʔνϯΛ࣮ߦ͢Δʹ͸
    $PSPVUJOF4DPQF͕ඞཁ
    wϥΠϑαΠΫϧΛ࣋ͭίϯϙʔωϯτͰ
    $PSPVUJOF4DPQFΛ࣮૷͢Δ
    w࣮ߦ؀ڥͷઃఆͱΩϟϯηϧॲཧΛ͢Δ

    View full-size slide

  62. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ฒߦॲཧΛॻ͘
    ௚ྻɺฒྻɺείʔϓͱྫ֎

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  68. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺ͱ͸
    wεϨουΛϒϩοΩϯά͢Δ͜ͱͳ͘ॲཧΛఀ
    ࢭͰ͖Δؔ਺

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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Λฦؔ͢਺

    View full-size slide

  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ؔ਺

    View full-size slide

  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ؔ਺
    ͜͜Ͱ͸ϫʔΧʔεϨουͰಈ͘ઃఆΛ͍ͯ͠Δ

    View full-size slide

  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*ϦΫΤετɻϒϩοΩϯά͢Δॲཧ

    View full-size slide

  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.BJOKPCͱ͢Δ

    View full-size slide

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

    View full-size slide

  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)
    }
    }
    }
    ᶃݺͼग़ͯ͠தஅ͢Δ

    View full-size slide

  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)
    }
    }
    }
    ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞

    View full-size slide

  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)
    }
    }
    }
    ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞
    ᶄ࠶։ͯ͠୅ೖ

    View full-size slide

  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*εϨουͰಈ࡞

    View full-size slide

  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ʹ൓ө

    View full-size slide

  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ʹ൓ө
    ྫ֎࣌͸ͬͪ͜

    View full-size slide

  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ʹ൓ө
    ྫ֎࣌͸ͬͪ͜
    Ωϟϯηϧ είʔϓ͕ऴྃͨ͠
    ͸ͬͪ͜

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  89. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ඇಉظॲཧΛॻ͘
    wXJUI$POUFYUؔ਺Λ࢖ͬͯίϧʔνϯͷ࣮ߦ
    ؀ڥΛ੾Γସ͑Δ
    w௚ྻͰ࣮ߦ͢Δ৔߹͸୯७ʹฒ΂Δ͚ͩͰྑ͍
    wΤϥʔͱΩϟϯηϧ͸USZDBUDIͰॲཧ͢Δ
    wඇಉظॲཧͷ΄ͱΜͲ͸͜ΕͰରԠͰ͖Δ͸ͣ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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ؔ਺Λ࢖ͬͯ
    ࢠείʔϓΛ։࢝͢Δ

    View full-size slide

  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ؔ਺ͰίϧʔνϯΛෳ਺ىಈ͢Δ

    View full-size slide

  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)
    }
    }
    }
    ଴ͪ߹ΘͤΔ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  97. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ฒߦੑͷߏ଄Խ
    IUUQTNFEJVNDPN!FMJ[BSPWTUSVDUVSFEDPODVSSFODZEBB

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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
    }
    }

    View full-size slide

  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ͰϑΝΠϧΛ։͘

    View full-size slide

  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ࡁ
    ΈͳͷͰΤϥʔʹͳΔ

    View full-size slide

  103. ©2018 Lang-8 Inc. ALL Rights Reserved.
    wฒߦ෼ذͷ։࢝ͱऴྃΛͻͱ·ͱ·Γͷ୯Ґ
    ʹͯ͠ɺͦΕΒΛ֊૚ߏ଄ʹ͢Δ
    ฒߦੑʹ͓͚ΔHPUPΛ๷͙ΞΠσΞ
    ฒߦ෼ذͷ·ͱ·Γ
    $PSPVUJOF4DPQF

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    BTZOD
    BTZOD

    View full-size slide

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

    BTZOD
    BTZOD

    View full-size slide

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

    BTZOD
    BTZOD

    είʔϓ಺ͷฒߦ෼ذͷ͍ͣΕ͔͕ΤϥʔʹͳΔͱ
    είʔϓશମ͕Τϥʔ Ωϟϯηϧ
    ʹͳΔ

    View full-size slide

  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

    View full-size slide

  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)
    }
    }
    }
    ࢠείʔϓ͕ऴྃ͠ྫ֎͕ඈΜͰ͘Δ

    View full-size slide

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

    View full-size slide

  113. ©2018 Lang-8 Inc. ALL Rights Reserved.
    1BSFOU4DPQF MBVODI

    BTZOD
    BTZOD

    DPSPVUJOF4DPQFؔ਺͕ͳ͔ͬͨΒ

    View full-size slide

  114. ©2018 Lang-8 Inc. ALL Rights Reserved.
    1BSFOU4DPQF MBVODI

    BTZOD
    BTZOD

    DPSPVUJOF4DPQFؔ਺͕ͳ͔ͬͨΒ

    View full-size slide

  115. ©2018 Lang-8 Inc. ALL Rights Reserved.
    ฒྻॲཧΛॻ͘
    wίϧʔνϯͷதͰίϧʔνϯ ෼ذ
    Λىಈ͢Δ৔
    ߹͸DPSPVUJOF4DPQFؔ਺Λ࢖͏ είʔϓͷ
    ֊૚ߏ଄Λߟ͑Δ

    wMBVODIBTZODΛωετͯ͠Δ࣌͸஫ҙ
    w෼ذ͕Τϥʔʹͳͬͯ΋είʔϓΛܧଓͰ͖Δ
    TVQFSWJTPS4DPQFؔ਺͕͋Δ ࠓճ͸লུ

    IUUQTLPUMJOHJUIVCJPLPUMJOYDPSPVUJOFTLPUMJOYDPSPVUJOFTDPSFLPUMJOYDPSPVUJOFTTVQFSWJTPSTDPQFIUNM

    View full-size slide

  116. ©2018 Lang-8 Inc. ALL Rights Reserved.
    3YXJUI$PSPVUJOF

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  122. ©2018 Lang-8 Inc. ALL Rights Reserved.
    3Y+BWBͱͷ౷߹
    wLPUMJOYDPSPVUJOFTSYΛ࢖͏
    w4JOHMF .BZCF $PNQMFUBCMFͳͲΛ
    TVTQFOEؔ਺ʹͰ͖Δ
    wTVTQFOEؔ਺Λ4JOHMF .BZCF
    $PNQMFUBCMFͳͲͰ͖Δ
    IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTUSFFNBTUFSSFBDUJWFLPUMJOYDPSPVUJOFTSY

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  126. ©2018 Lang-8 Inc. ALL Rights Reserved.
    TVTQFOEؔ਺Λ4JOHMFʹ͢Δ
    fun getProfile(id: Int): Single {
    return rxSingle {
    Profile(id)
    }
    }
    TVTQFOEϥϜμΛड͚औͬͯ݁ՌΛ
    4JOHMFͰฦؔ͢਺
    Τϥʔʹͳͬͯ΋਌είʔϓΛ
    Ωϟϯηϧ͠ͳ͍

    View full-size slide

  127. ©2018 Lang-8 Inc. ALL Rights Reserved.
    3Y+BWBXJUI$PSPVUJPOF
    w LPUMJOYDPSPVUJOFTSYͰ૬ޓͷΠϯςάϨʔγϣ
    ϯ͕Մೳ
    w ྫ͑͹ෳࡶʹ߹੒͕ඞཁͳՕॴͳͲ͸ίϧʔνϯͰϑϥο
    τʹॻ͍ͯ࠷ޙʹ4JOHMFʹ͢Δͱ͔͕͍͍͔΋
    w ରཱ͢Δ΋ͷͰ͸ͳ͍ͷͰ͍͍ײ͡ʹ࢖͏ͷ͕ྑ͍ ͓͢
    ͢Ί͸4JOHMF .BZCF $PNQMFUBCMFͷஔ͖׵͑

    View full-size slide

  128. ©2018 Lang-8 Inc. ALL Rights Reserved.
    +FUQBDLXJUI$PSPVUJOF

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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ؔ਺ʹͳΔ

    View full-size slide

  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

    View full-size slide

  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Λ໌ࣔతʹ౉͢ඞཁ͕͋Δ

    View full-size slide

  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;
    }

    View full-size slide

  135. ©2018 Lang-8 Inc. ALL Rights Reserved.
    3PPNͱίϧʔνϯ
    w BMQIBͰ%"0ͰTVTQFOEؔ਺Λαϙʔτ

    View full-size slide

  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
    @Delete
    suspend fun delete(user: User)
    }

    View full-size slide

  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
    }
    }

    View full-size slide

  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ؔ਺͕ݺͼग़͞ΕΔ

    View full-size slide

  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ʹ͍ͯ͠Δ

    View full-size slide

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

    View full-size slide

  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ΛઃఆͰ͖Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide