It’s never easy to write async code but luckily there are many libraries to manage asynchronicity without adding too much complexity. In the last years RxJava and the other ReactiveX libraries have been very popular but lately there is a new way to manage async code in Kotlin: the coroutines. In this talk we’ll see pros and cons of there two approaches and how to leverage them to simplify asynchronous code on Android. Do they solve the same problem? Can we use them together? How can we use them effectively in Android development? Spoiler alert: They are both great!
25. class MyViewModel(
private val service: StackOverflowService
) : ViewModel() {
private val disposable = CompositeDisposable()
fun load() {
disposable +=
service.getTopUsers()
.subscribeOn(io())
.observeOn(mainThread())
.subscribe(
{ users -> updateUi(users) },
{ e -> updateUi(e) }
)1
}A
private fun updateUi(s: Any) {
//...
}B
override fun onCleared() {
disposable.clear()
}C
}D
class MyViewModel(
private val service: StackOverflowService
) : ViewModel() {
fun load() {
viewModelScope.launch {
try {
val users = service.getTopUsers()
updateUi(users)
} catch (e: Exception) {
updateUi(e)
}4
}5
}E
private fun updateUi(s: Any) {
//...
}G
}H
RxJava Coroutines
26. class MyViewModel(
private val service: StackOverflowService
) : ViewModel() {
private val disposable = CompositeDisposable()
fun load() {
disposable +=
service.getTopUsers()
.subscribeOn(io())
.observeOn(mainThread())
.subscribe(
{ users -> updateUi(users) },
{ e -> updateUi(e) }
)1
}A
private fun updateUi(s: Any) {
//...
}B
override fun onCleared() {
disposable.clear()
}C
}D
class MyViewModel(
private val service: StackOverflowService
) : ViewModel() {
fun load() {
viewModelScope.launch {
try {
val users = service.getTopUsers()
updateUi(users)
} catch (e: Exception) {
updateUi(e)
}4
}5
}E
private fun updateUi(s: Any) {
//...
}G
}H
RxJava Coroutines
viewModelScope
27. viewModelScope
Uses Dispatchers.Main
doesn’t block the Main thread executing a suspending method
Connected to the ViewModel lifecycle
cancels the Jobs when the ViewModel is destroyed
not available yet (but easy to implement)
android-review.googlesource.com/c/platform/frameworks/support/+/772083
28. class MyViewModel(
private val service: StackOverflowService
) : ViewModel() {
private val disposable = CompositeDisposable()
fun load() {
disposable +=
service.getTopUsers()
.subscribeOn(io())
.observeOn(mainThread())
.subscribe(
{ users -> updateUi(users) },
{ e -> updateUi(e) }
)1
}A
private fun updateUi(s: Any) {
//...
}B
override fun onCleared() {
disposable.clear()
}C
}D
class MyViewModel(
private val service: StackOverflowService
) : ViewModel() {
fun load() {
viewModelScope.launch {
try {
val users = service.getTopUsers()
updateUi(users)
} catch (e: Exception) {
updateUi(e)
}4
}5
}E
private fun updateUi(s: Any) {
//...
}G
}H
RxJava Coroutines
35. RxJava Coroutines
Schedulers
subscribeOn defines
when the computation
starts
observeOn changes the
thread (until next
observeOn)
Dispatchers
the computation starts on
the context dispatcher
withContext changes the
thread in a block
87. My algorithm:
If you are already using RxJava and it works for you ->
RxJava
If the architecture is based on reactive stream -> RxJava
If the project is multiplatform with Kotlin Native ->
Coroutines
If the codebase is Java/Kotlin -> RxJava
else -> Coroutines
RxJava and coroutines are both great,
which one should I use?
88. Links
Demo Project
github.com/fabioCollini/RxJavaVsCoroutines
Guide to kotlinx.coroutines by example
github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md
Kotlin coroutines, a deeper look
medium.com/@elizarov/kotlin-coroutines-a-deeper-look-180536305c3f
Async code using Kotlin Coroutines
proandroiddev.com/async-code-using-kotlin-coroutines-233d201099ff
ViewModelScope
android-review.googlesource.com/c/platform/frameworks/support/+/772083
Retrofit suspending methods support
github.com/square/retrofit/pull/2886