Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
3
880
AndroidアプリのAI実装をAndroidifyで学ぶ ー Google公式サンプルによる体験と実装 ー
takahirom
0
160
AIエージェントによるテストフレームワーク Arbigent
takahirom
0
1.1k
Google の LLM ライブラリ を Android アプリで 使うには?
takahirom
1
2.2k
Robolectric Native Graphics and Roborazzi
takahirom
1
2.4k
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
Mobile First: as difficult as doing things right
swwweet
225
10k
The Curious Case for Waylosing
cassininazir
1
510
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
570
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
76k
Into the Great Unknown - MozCon
thekraken
41
2.7k
For a Future-Friendly Web
brad_frost
183
10k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
830
YesSQL, Process and Tooling at Scale
rocio
174
15k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
500
Accessibility Awareness
sabderemane
1
210
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 かわいい