Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain about downstream and upstream in rxJava

Tags:

java

android

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

like image 368
Bulma Avatar asked Nov 23 '18 06:11

Bulma


1 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.

like image 51
Kharda Avatar answered Sep 28 '22 03:09

Kharda