6. Rx
• Create
Easily create event streams or data streams.
• Combine
Compose and transform streams with query-like operators.
• Listen
Subscribe to any observable stream to perform side effects.
9. RxJava event/data stream
• Observable
0..N flows, no backpressure,
• Flowable
0..N flows, supporting Reactive-Streams and backpressure
10. RxJava event/data stream
• Single
a flow of exactly 1 item or an error,
• Completable
a flow without items but only a completion or error signal,
• Maybe
a flow with no items, exactly one item or an error.
16. Coroutine scope
A fundamental building block of coroutines.
• threading
• life-cycle
object GlobalScope : CoroutineScope
17. Coroutine builder
Starts coroutine from a regular non-suspending scope.
fun CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job
fun <T> CoroutineScope.async(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
): Deferred<T>
18. Coroutine dispatcher
Ensures that the execution of a coroutine gets dispatched on
the right thread.
• Dispatchers.Main
• Dispatchers.Default
• Dispatchers.IO
• Dispatchers.Unconfined
19. suspend function
Suspends execution of the code without blocking the current
thread of execution.
suspend fun loadSticker(id: Int): Sticker = ...
21. Compare RxJava2 stream with
Coroutines
RxJava2 Coroutines
Observable ReceiveChannel (experimental)
Flowable ReceiveChannel (experimental)
Single Deferred
Completable Job
Maybe Deferred
22. kotlinx-coroutines-rx2
• Modules that provide builders and iteration support for
various reactive streams libraries:
• Reactive Streams, RxJava 2.x, and Project Reactor
https://github.com/Kotlin/kotlinx.coroutines/blob/master/
reactive/kotlinx-coroutines-rx2
23. RxJava2 to Coroutines
suspend fun <T> SingleSource<T>.await(): T
suspend fun CompletableSource.await(): Unit
suspend fun <T> MaybeSource<T>.await(): T?
24. Coroutines to RxJava2
fun CoroutineScope.rxSingle(...): Single
fun CoroutineScope.rxCompletable(...): Completable
fun CoroutineScope.rxMaybe(...): Maybe