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

kanmoba-returns-02.pdf

 kanmoba-returns-02.pdf

Takuji Nishibayashi

June 12, 2019
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

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

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

    /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
  3. suspend function fetchUser() : User { val user = //

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

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

    in main thread delay(100) val response = withContext(Dispatchers.Default) { // run in background thread networkAccess() } }
  6. Ϋϥογϡ͢Δ΍ͭ // ྫ֎Ͱείʔϓશମ͕ࢮΜͰΫϥογϡ͢Δ coroutineScope { try { val user =

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

    series = async { fetchSeries("12345") } // Deferred<Series> doSomething(user.await(), series.await())
  9. val channel = Channel<User>(Channel.CONFLATED) userObserver.addOnChangeListener { user -> launch {

    channel.send(user) } } channel.consumeEach { user -> doSomething(user) }
  10. val userFlow = flow<User> { emit(observeUser()) } val seriesFlow =

    flow<Series> { emit(observeSeries()) } userFlow.combineLatest(seriesFlow) { user, series -> createItemModels(user, series) }.collect { itemModels -> // show items }