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

Androidの非同期処理をKotlinコルーチンで行う

yagi
June 12, 2017

 Androidの非同期処理をKotlinコルーチンで行う

yagi

June 12, 2017
Tweet

More Decks by yagi

Other Decks in Technology

Transcript

  1. ௚ྻͷ࣮ߦ val disposable = userApi.me() .flatMap { user -> subscriptionShopApi.getSubscriptionShops(user.id)

    } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { // success }, { // error } ) qBU.BQΛ࢖ͬͯ௚ྻʹ࣮ߦ͢Δ
  2. ฒྻͷ࣮ߦ val disposable = Single.zip<User, Shop, Pair<User, Shop>>( userApi.me().subscribeOn(Schedulers.io()), shopApi.getShop(10L).subscribeOn(Schedulers.io()),

    BiFunction { user, shop -> Pair(user, shop) } ) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { (user, shop) -> // success }, { // error } ) [JQΛ࢖ͬͯฒྻʹ࣮ߦ͢Δ
  3. ୯ൃͷ࣮ߦ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch (e: Exception) { // error } }
  4. ୯ൃͷ࣮ߦ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch (e: Exception) { // error } } Ϗϧμʔؔ਺ͱΑ͹ΕΔɺ ίϧʔνϯΛߏங͢Δؔ਺
  5. BTZODؔ਺ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() //…
  6. BTZODؔ਺ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() //… LPUMJOYDPSPVUJOFTDPSF͕ఏڙ͢Δ$PSPVUJOF$POUFYUɻ KBWBVUJMDPODVSSFOU'PSL+PJO1PPMDPNNPO1PPM Λ ࢖ͬͯεϨουϓʔϧΛ࡞Γɺ࠶։ॲཧΛεϨουͰ࣮ߦ͢Δɻ "OESPJE؀ڥ /FYVT9 Ͱ͸ϓʔϧαΠζ͸ͩͬͨɻ
  7. ୯ൃͷ࣮ߦ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch (e: Exception) { // error } }
  8. ୯ൃͷ࣮ߦ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch (e: Exception) { // error } } 6*εϨουͰ଴ͪ߹Θ͍͍ͤͯͷ͔ͳʜ
  9. ୯ൃͷ࣮ߦ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch (e: Exception) { // error } } %FGFSSFEBXBJU ͸தஅؔ਺
  10. தஅؔ਺ͱεςʔτϚγϯ w ίϯύΠϥ͕தஅؔ਺ͷݺͼग़͠Λঢ়ଶͱͯ͠෼ ղ͠ɺॲཧશମΛεςʔτϚγϯʹม׵͢Δɻ͜ ΕʹΑΓɺؔ਺ͷதஅͱ࠶։Λ࣮ݱ͍ͯ͠Δ launch(UI) { try { val

    shop = async(CommonPool) {…}.await() // success! } catch (e: Exception) { // error } } ঢ়ଶ async(CommonPool){…}.await() ঢ়ଶ val shop =… success ঢ়ଶ error
  11. ୯ൃͷ࣮ߦ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch (e: Exception) { // error } }
  12. ୯ൃͷ࣮ߦ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch (e: Exception) { // error } } ͪΐͬͱ৑௕ͩͳʜ
  13. ิॿతͳؔ਺Λ༻ҙ͢Δ // async(CommonPool)Λ؆ུԽ͢Δ fun <T> async(context: CoroutineContext = CommonPool, start:

    CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T) = kotlinx.coroutines.experimental.async(context, start, block) // launch(UI)Λ؆ུԽ͢Δ fun ui(start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit) = launch(UI, start, block)
  14. ୯ൃͷ࣮ߦ val job = ui { try { val shop

    = async { shopApi.getShop(10) }.await() // success } catch (e: Exception) { // error } }
  15. ௚ྻͷ࣮ߦ val job = ui { try { val userJob

    = async { userApi.me() } val subscriptionShopsJob = async { subscriptionShopApi.getSubscriptionShops(userJob.await().id) } val subscriptionShops = subscriptionShopsJob.await() // success } catch (e: Exception) { // error } }
  16. ௚ྻͷ࣮ߦ val job = ui { try { val userJob

    = async { userApi.me() } val subscriptionShopsJob = async { subscriptionShopApi.getSubscriptionShops(userJob.await().id) } val subscriptionShops = subscriptionShopsJob.await() // success } catch (e: Exception) { // error } } VTFS+PCͷ݁ՌΛ଴ͪ߹Θͤͨ͋ͱ࣮ߦ
  17. ฒྻͷ࣮ߦ val job = ui { try { val userJob

    = async { userApi.me() } val shopJob = async { shopApi.getShop(10L) } val user = userJob.await() val shop = shopJob.await() // success catch (e: Exception) { // error } }
  18. ฒྻͷ࣮ߦ val job = ui { try { val userJob

    = async { userApi.me() } val shopJob = async { shopApi.getShop(10L) } val user = userJob.await() val shop = shopJob.await() // success catch (e: Exception) { // error } } $PSPVUJOF4UBSU%&'"6-5Λ࢖͍ͬͯΔͷͰ ͦΕͧΕ$PNNPO1PPMͰ௚ͪʹ࣮ߦ͞ΕΔ
  19. Ωϟϯηϧ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch(e: CancellationException) { // cancel } catch (e: Exception) { // error } } job.cancel()
  20. Ωϟϯηϧ val job = launch(UI) { try { val shop

    = async(CommonPool) { shopApi.getShop(10) }.await() // success! } catch(e: CancellationException) { // cancel } catch (e: Exception) { // error } } job.cancel() ྫ֎ͱͯ͠ॲཧ
  21. ײ૝ w େମ͍͚Δ ஔ͖׵͑ͯ΋Αͦ͞͏  w ௚ྻɺฒྻͷ࢖͍෼͚͕؆୯ w εϨουͷ؅ཧ͕͍ۙ৔ॴʹ͋Δ w

    ΤϥʔɺΩϟϯηϧ͕શͯྫ֎ͳͷͰϋϯυϦϯ ά͠΍͍͢ w ίϧʔνϯ΋3Y+BWBͱಉ͘͡Β͍Ԟ͕ਂ͍ͷͰ ͦ͜·Ͱؾܰʹ΍ΔͬͯΘ͚ʹ͸͍͔ͳ͍͚Ͳศ རͰ࠷ߴ
  22. ࢀߟࢿྉ w ࠓճͷίʔυ w IUUQTHJUIVCDPNTZTZBHJJNQMFNFOUBUJPOPGBTZODSFRVFTU XJUISYKBWBPSDPSPVUJOF w $PSPVUJOFTGPS,PUMJO 3FWJTJPO 

    w IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJO DPSPVUJOFTJOGPSNBMNE w (VJEFUPLPUMJOYDPSPVUJOFTCZFYBNQMF w IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTCMPCNBTUFS DPSPVUJOFTHVJEFNE w (VJEFUP6*QSPHSBNNJOHXJUIDPSPVUJOFT w IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTCMPCNBTUFSVJ DPSPVUJOFTHVJEFVJNE