Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

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

Avatar for takahirom takahirom
August 25, 2018
1.2k

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

Avatar for takahirom

takahirom

August 25, 2018
Tweet

More Decks by takahirom

Transcript

  1. 前提の説明: RxJavaの例 class ViewModel( private val api: Api = Api(),

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

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

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

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

    private val scheduler: Scheduler = Schedulers.io() ) { private val mutablePersons = MutableLiveData<List<Person>>() val persons: LiveData<List<Person>> = mutablePersons fun onCreate() { api.fetchPersons() .subscribeOn(scheduler) .subscribe(mutablePersons::postValue) }} }} 結果をpostValueして、後はLiveDataに任せる
  6. kotlinx-coroutines-rx2で 変換する class ViewModel( private val api: Api = Api(),

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

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

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

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

    private val coroutineContext: CoroutineContext = CommonPool ) { private val mutablePersons = MutableLiveData<List<Person>>() val persons: LiveData<List<Person>> = mutablePersons fun onCreate() { launch(coroutineContext) { val persons = api.fetchPersons().await() mutablePersons.postValue(persons) }} }} }} あとは同じようにLiveDataに流すだけ