kanmoba-returns-02.pdf

 kanmoba-returns-02.pdf

E28404a128b6014e6f499d2a967c72a0?s=128

Takuji Nishibayashi

June 12, 2019
Tweet

Transcript

  1. 3.

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

    Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ
  2. 4.

    ࣗݾ঺հ • Android • Kotlin • Flu-er • ! h-ps:/

    /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
  3. 11.

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

    ωοτϫʔΫ௨৴ͳͲͷॏ͍ॲཧ return user }
  4. 15.

    Job

  5. 17.
  6. 20.

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

    in main thread delay(100) val response = withContext(Dispatchers.Default) { // run in background thread networkAccess() } }
  7. 26.

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

    async { fetchUser() } val series = async { fetchSeries("12345") } doSomething(user.await(), series.await()) } catch (e: IOException) { // catch͞Εͳ͍ } }
  8. 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 } }
  9. 28.
  10. 30.

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

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

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

    channel.send(user) } } channel.consumeEach { user -> doSomething(user) }
  13. 34.
  14. 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 }