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

kanmoba-returns-02.pdf

 kanmoba-returns-02.pdf

E28404a128b6014e6f499d2a967c72a0?s=128

Takuji Nishibayashi

June 12, 2019
Tweet

Transcript

  1. 5෼ͰΘ͔ͬͨؾʹͳΔKotlin Corou,nes @takuji31 ؼ͖ͬͯͨؔ੢ϞόΠϧΞϓϦݚڀձ #2

  2. ࣗݾ঺հ

  3. ࣗݾ঺հ • @takuji31 id:takuji31 • Takuji Nishibayashi • Hatena Co.,

    Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ
  4. ࣗݾ঺հ • Android • Kotlin • Flu-er • ! h-ps:/

    /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
  5. Kotlin Corou+nes

  6. ࢖ͬͯ·͔͢ʁ

  7. ίϧʔνϯʢӳ: co-rou(neʣͱ͸ϓϩάϥ ϛϯάͷߏ଄ͷҰछɻαϒϧʔνϯ͕Τϯτ Ϧʔ͔ΒϦλʔϯ·ͰΛҰͭͷॲཧ୯Ґͱ͢ Δͷʹର͠ɺίϧʔνϯ͸͍ͬͨΜॲཧΛ தஅͨ͠ޙɺଓ͖͔ΒॲཧΛ࠶։Ͱ͖Δɻ — h$ps://ja.wikipedia.org/wiki/ %E3%82%B3%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3

  8. Corou%neʹ͍͓ͭͯ͞Β͍͠· ͠ΐ͏

  9. suspended func*on

  10. தஅՄೳͳؔ਺

  11. suspend function fetchUser() : User { val user = //

    ωοτϫʔΫ௨৴ͳͲͷॏ͍ॲཧ return user }
  12. Corou%neContext

  13. Corou%neͷίϯςΩετ

  14. ෳ਺ͷElementΛ૊Έ߹Θͤͯ࡞Δ

  15. Job

  16. Dispatcher

  17. Name

  18. Excep&onHandler

  19. ωετՄೳ

  20. val context = Job() + Dispatchers.Main launch(context) { // run

    in main thread delay(100) val response = withContext(Dispatchers.Default) { // run in background thread networkAccess() } }
  21. Corou%neScope

  22. Corou%neͷείʔϓ

  23. ContextΛ࣋ͭ

  24. ωετՄೳ

  25. async/launchͰ্ཱͪ͛ͨίϧʔν ϯ಺Ͱྫ֎͕ى͖Δͱείʔϓશମ ͕ࢮ͵ͷͰ஫ҙ

  26. Ϋϥογϡ͢Δ΍ͭ // ྫ֎Ͱείʔϓશମ͕ࢮΜͰΫϥογϡ͢Δ coroutineScope { try { val user =

    async { fetchUser() } val series = async { fetchSeries("12345") } doSomething(user.await(), series.await()) } catch (e: IOException) { // catch͞Εͳ͍ } }
  27. ରࡦ coroutineScope { try { // Nested scope val (user,

    series) = coroutineScope { val user = async { fetchUser() } val series = async { fetchSeries("12345") } Pair(user, series) } doSomething(user.await(), series.await()) } catch (e: IOException) { // show error } }
  28. Deferred

  29. ϊϯϒϩοΩϯάͳFuture

  30. val user = async { fetchUser() } // Deferred<User> val

    series = async { fetchSeries("12345") } // Deferred<Series> doSomething(user.await(), series.await())
  31. Channel

  32. ϗοτͳετϦʔϜ

  33. val channel = Channel<User>(Channel.CONFLATED) userObserver.addOnChangeListener { user -> launch {

    channel.send(user) } } channel.consumeEach { user -> doSomething(user) }
  34. Flow

  35. ίʔϧυͳετϦʔϜ

  36. Preview status

  37. 1.3.0-M1ͰExperimentalʹ

  38. val userFlow = flow<User> { emit(observeUser()) } val seriesFlow =

    flow<Series> { emit(observeSeries()) } userFlow.combineLatest(seriesFlow) { user, series -> createItemModels(user, series) }.collect { itemModels -> // show items }
  39. Enjoy Kotlin Corou.nes life!