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

RxJavaを使っている 既存アプリに Kotlin Coroutinesを導入しよう

takahirom
August 25, 2018
930

RxJavaを使っている 既存アプリに Kotlin Coroutinesを導入しよう

takahirom

August 25, 2018
Tweet

Transcript

  1. RxJavaを使っている
    既存アプリに
    Kotlin Coroutinesを導⼊しよう
    takahirom
    CyberAgent, Inc.

    View Slide

  2. みなさんのアプリは

    だいたいRxJava
    使ってますよね?

    View Slide

  3. 実際のアプリでは

    Observableより
    Single, Completable, Maybeが

    多いのでは?

    View Slide

  4. SingleなどよりKotlin Coroutinesの
    suspend functionのほうが優れている

    View Slide

  5. SingleなどはCoroutineに
    置き換えたい!

    View Slide

  6. 置き換えるサンプルを

    ⽤意してみました。

    View Slide

  7. こんな感じのサンプル
    7JFX
    7JFX.PEFM
    Method call
    "QJ
    Single>
    LiveData>

    View Slide

  8. Singleをsuspend functionに
    7JFX
    7JFX.PEFM
    "QJ
    Single>
    LiveData>
    7JFX
    7JFX.PEFM
    "QJ
    suspend List
    LiveData>

    View Slide

  9. 前提の説明: RxJavaの例
    class ViewModel(
    private val api: Api = Api(),
    private val scheduler: Scheduler = Schedulers.io()
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    api.fetchPersons()
    .subscribeOn(scheduler)
    .subscribe(mutablePersons::postValue)
    }}
    }}

    View Slide

  10. 前提の説明: RxJavaの例
    class ViewModel(
    private val api: Api = Api(),
    private val scheduler: Scheduler = Schedulers.io()
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    api.fetchPersons()
    .subscribeOn(scheduler)
    .subscribe(mutablePersons::postValue)
    }}
    }}
    Activityから呼び出す

    View Slide

  11. 前提の説明: RxJavaの例
    class ViewModel(
    private val api: Api = Api(),
    private val scheduler: Scheduler = Schedulers.io()
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    api.fetchPersons()
    .subscribeOn(scheduler)
    .subscribe(mutablePersons::postValue)
    }}
    }}
    Api#fetchPersonsがSingleを返してくる

    View Slide

  12. 前提の説明: RxJavaの例
    class ViewModel(
    private val api: Api = Api(),
    private val scheduler: Scheduler = Schedulers.io()
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    api.fetchPersons()
    .subscribeOn(scheduler)
    .subscribe(mutablePersons::postValue)
    }}
    }}
    schedulerで実⾏

    View Slide

  13. 前提の説明: RxJavaの例
    class ViewModel(
    private val api: Api = Api(),
    private val scheduler: Scheduler = Schedulers.io()
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    api.fetchPersons()
    .subscribeOn(scheduler)
    .subscribe(mutablePersons::postValue)
    }}
    }}
    結果をpostValueして、後はLiveDataに任せる

    View Slide

  14. 置き換えていこう

    View Slide

  15. 実際のコードだと同じSingleなメソッドを

    いくつものところから⾒ていたりする
    7JFX
    7JFX.PEFM
    "QJ
    LiveData>
    7JFX
    7JFX.PEFM
    LiveData>
    Single>
    ⼀つだけ置き換えできない
    suspend fun List

    View Slide

  16. kotlinx-coroutines-rx2
    で変換できる

    View Slide

  17. kotlinx-coroutines-rx2で
    変換する
    7JFX
    7JFX.PEFM
    "QJ
    Single>
    LiveData>
    7JFX
    7JFX.PEFM
    LiveData>
    Single>
    single.await()

    View Slide

  18. kotlinx-coroutines-rx2で
    変換する
    class ViewModel(
    private val api: Api = Api(),
    private val coroutineContext: CoroutineContext = CommonPool
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    launch(coroutineContext) {
    val persons = api.fetchPersons().await()
    mutablePersons.postValue(persons)
    }}
    }}
    }}

    View Slide

  19. kotlinx-coroutines-rx2で
    変換する
    class ViewModel(
    private val api: Api = Api(),
    private val coroutineContext: CoroutineContext = CommonPool
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    launch(coroutineContext) {
    val persons = api.fetchPersons().await()
    mutablePersons.postValue(persons)
    }}
    }}
    }} Coroutines起動

    View Slide

  20. kotlinx-coroutines-rx2で
    変換する
    class ViewModel(
    private val api: Api = Api(),
    private val coroutineContext: CoroutineContext = CommonPool
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    launch(coroutineContext) {
    val persons = api.fetchPersons().await()
    mutablePersons.postValue(persons)
    }}
    }}
    }} CoroutineContextとSchedulerは似たようなもの

    View Slide

  21. kotlinx-coroutines-rx2で
    変換する
    class ViewModel(
    private val api: Api = Api(),
    private val coroutineContext: CoroutineContext = CommonPool
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    launch(coroutineContext) {
    val persons = api.fetchPersons().await()
    mutablePersons.postValue(persons)
    }}
    }}
    }}
    Singleを中断関数に変換できる
    そして普通にListが取得できる

    View Slide

  22. kotlinx-coroutines-rx2で
    変換する
    class ViewModel(
    private val api: Api = Api(),
    private val coroutineContext: CoroutineContext = CommonPool
    ) {
    private val mutablePersons = MutableLiveData>()
    val persons: LiveData> = mutablePersons
    fun onCreate() {
    launch(coroutineContext) {
    val persons = api.fetchPersons().await()
    mutablePersons.postValue(persons)
    }}
    }}
    }}
    あとは同じようにLiveDataに流すだけ

    View Slide

  23. いけそう!

    View Slide

  24. 実際にプロダクションに
    導⼊していきたい!

    View Slide

  25. 実際にプロダクションに
    導⼊していくには
    テストとか
    ライフサイクルとか
    エラーハンドリングとか必要

    View Slide

  26. • テスト
    • エラーハンドリング
    • ライフサイクル
    • RxJavaのスレッドとの競合
    実際にプロダクションに
    導⼊していこう!
    → Qiita 『RxJavaを使っている既存アプリに
    Kotlin Coroutinesを導⼊しよう』にて!

    View Slide

  27. • https://github.com/
    takahirom/rxjava-2-kotlion-
    coroutines
    • いい感じにモジュール分かれ
    ていて、テストもあります
    サンプルコードもあります

    View Slide

  28. 要はRxJava1 -> RxJava2
    の置き換えと⼀緒

    View Slide

  29. Kotlin かわいい

    View Slide