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
780
AndroidアプリのAI実装をAndroidifyで学ぶ ー Google公式サンプルによる体験と実装 ー
takahirom
0
150
AIエージェントによるテストフレームワーク Arbigent
takahirom
0
1.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.3k
Jetpack Compose State Practices
takahirom
1
1.5k
Inside Jetpack Compose
takahirom
1
1.1k
Featured
See All Featured
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
370
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Become a Pro
speakerdeck
PRO
31
6.2k
Discover your Explorer Soul
emna__ayadi
2
1.2k
Claude Code のすすめ
schroneko
67
230k
Producing Creativity
orderedlist
PRO
348
40k
The browser strikes back
jonoalderson
0
1.5k
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 かわいい