Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RxJavaを使っている 既存アプリに Kotlin Coroutinesを導入しよう
Search
takahirom
August 25, 2018
1.2k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RxJavaを使っている 既存アプリに Kotlin Coroutinesを導入しよう
takahirom
August 25, 2018
More Decks by takahirom
See All by takahirom
AIエージェントで 変わるAndroid開発環境
takahirom
2
610
AndroidアプリのAI実装をAndroidifyで学ぶ ー Google公式サンプルによる体験と実装 ー
takahirom
0
140
AIエージェントによるテストフレームワーク Arbigent
takahirom
0
1k
Google の LLM ライブラリ を Android アプリで 使うには?
takahirom
1
2.2k
Robolectric Native Graphics and Roborazzi
takahirom
1
2.3k
Androidアプリで安定して動作させ継続的に開発するために設計の原則を利用して開発した話
takahirom
3
1.5k
Android Tools & Performance
takahirom
1
1.2k
Jetpack Compose State Practices
takahirom
1
1.5k
Inside Jetpack Compose
takahirom
1
1.1k
Featured
See All Featured
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
150
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.4k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
900
Exploring anti-patterns in Rails
aemeredith
3
440
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
Code Review Best Practice
trishagee
74
20k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
320
Scaling GitHub
holman
464
140k
[SF Ruby Conf 2025] Rails X
palkan
2
1.2k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
370
Docker and Python
trallard
47
3.9k
Transcript
RxJavaを使っている 既存アプリに Kotlin Coroutinesを導⼊しよう takahirom CyberAgent, Inc.
みなさんのアプリは だいたいRxJava 使ってますよね?
実際のアプリでは Observableより Single, Completable, Maybeが 多いのでは?
SingleなどよりKotlin Coroutinesの suspend functionのほうが優れている
SingleなどはCoroutineに 置き換えたい!
置き換えるサンプルを ⽤意してみました。
こんな感じのサンプル 7JFX 7JFX.PEFM Method call "QJ Single<List<Person>> LiveData<List<Person>>
Singleをsuspend functionに 7JFX 7JFX.PEFM "QJ Single<List<Person>> LiveData<List<Person>> 7JFX 7JFX.PEFM "QJ
suspend List<Person> LiveData<List<Person>>
前提の説明: 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) }} }}
前提の説明: 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から呼び出す
前提の説明: 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を返してくる
前提の説明: 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で実⾏
前提の説明: 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に任せる
置き換えていこう
実際のコードだと同じSingleなメソッドを いくつものところから⾒ていたりする 7JFX 7JFX.PEFM "QJ LiveData<List<Person>> 7JFX 7JFX.PEFM LiveData<List<Person>> Single<List<Person>>
⼀つだけ置き換えできない suspend fun List<Person>
kotlinx-coroutines-rx2 で変換できる
kotlinx-coroutines-rx2で 変換する 7JFX 7JFX.PEFM "QJ Single<List<Person>> LiveData<List<Person>> 7JFX 7JFX.PEFM LiveData<List<Person>>
Single<List<Person>> single.await()
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) }} }} }}
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起動
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は似たようなもの
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>が取得できる
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に流すだけ
いけそう!
実際にプロダクションに 導⼊していきたい!
実際にプロダクションに 導⼊していくには テストとか ライフサイクルとか エラーハンドリングとか必要
• テスト • エラーハンドリング • ライフサイクル • RxJavaのスレッドとの競合 実際にプロダクションに 導⼊していこう!
→ Qiita 『RxJavaを使っている既存アプリに Kotlin Coroutinesを導⼊しよう』にて!
• https://github.com/ takahirom/rxjava-2-kotlion- coroutines • いい感じにモジュール分かれ ていて、テストもあります サンプルコードもあります
要はRxJava1 -> RxJava2 の置き換えと⼀緒
Kotlin かわいい