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.5k
Inside Jetpack Compose
takahirom
1
1.1k
What’s new in Android Jetpack and Tools
takahirom
0
430
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
140
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
490
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
220
We Are The Robots
honzajavorek
0
250
Mind Mapping
helmedeiros
PRO
1
250
Darren the Foodie - Storyboard
khoart
PRO
3
3.4k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
240
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
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 かわいい