Home » Android » java – Explain about downstream and upstream in rxJava

java – Explain about downstream and upstream in rxJava

Posted by: admin June 15, 2020 Leave a comment

Questions:

I don’t really understand the meaning of downstream and upstream in Rxjava.
What does that mean? Is it equivalent to subscribeOn and observableOn?

How to&Answers:

We can divide stream by seeing its position based on an operator.

         upstream          downstream
source <--------- operator -----------> consumer/further operators

So from top to the operator, we call it upstream.

From operator to the bottom, we call it downstream.

It’s not equivalent to subscribeOn and observeOn. subscribeOn and observeOn are only operators. However, we can distinguish the behaviour of that two operators by using downstream and upstream concept.

subscribeOn is affecting its upstream and downstream. For example, subcsribeOn 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))

will make all the stream (up and down) run on computation thread.

On the other hand, observeOn only affecting 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 make the downstream run on computation thread.

I hope the explanation will help.