Explains downstream and upstream in rxJava
I don’t quite understand what downstream and upstream mean in Rxjava.
What does that mean? Is it equivalent to subscribeOn and observableOn?
Solution
We can divide streams based on where the operator sees them.
upstream downstream
source <--------- operator -----------> consumer/further operators
So from top to operator, we call it upstream.
From the operator to the bottom, we call it downstream.
It is not equivalent to subscribeOn
and observeOn
. subscribeOn
and observeOn
are just operators. However, we can distinguish the behavior of these two operators by the concept of downstream and upstream.
subscribeOn
is affecting both upstream and downstream of it. For example, subcsribeOn
is on this code
just("Some String")
.map(str -> str.length())
.subsribeOn(Schedulers.computation()) // change thread
.map(length -> 2 * length)
.subscribe(number -> Log.d("", "Number " + number))
All streams (up and down) will be made to run on the compute thread.
On the other hand, observeOn
only affects downstream. observeOn
on this code
just("Some String")
.map(str -> str.length())
.observeOn(Schedulers.computation()) // change thread
.map(length -> 2 * length)
.subscribe(number -> Log.d("", "Number " + number))
Only let downstream run on the compute thread.
Hope the explanation helps you.