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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
AndroidアプリのAI実装をAndroidifyで学ぶ ー Google公式サンプルによる体験と実装 ー
takahirom
0
140
AIエージェントによるテストフレームワーク Arbigent
takahirom
0
1k
Google の LLM ライブラリ を Android アプリで 使うには?
takahirom
1
2.1k
Robolectric Native Graphics and Roborazzi
takahirom
1
2.3k
Androidアプリで安定して動作させ継続的に開発するために設計の原則を利用して開発した話
takahirom
3
1.4k
Android Tools & Performance
takahirom
1
1.2k
Jetpack Compose State Practices
takahirom
1
1.4k
Inside Jetpack Compose
takahirom
1
1.1k
What’s new in Android Jetpack and Tools
takahirom
0
430
Featured
See All Featured
Writing Fast Ruby
sferik
630
63k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
310
Become a Pro
speakerdeck
PRO
31
6k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
330
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
エンジニアに許された特別な時間の終わり
watany
107
250k
The browser strikes back
jonoalderson
0
1.2k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
160
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
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 かわいい