Understanding Schedulers
There is an incorrect misconception and belief that RxJava
processing is multithreaded by default. An Observable
and the list of transformations applied by the specified operators occur on the same thread that the subscription is made.
Hence, on Android, if the subscription is carried out on the main thread, the operators chain processing will run on the main thread, blocking the UI until the work is done.
While this behavior might work for lightweight processing tasks, when the operation requires IO interaction or CPU-intensive computing, the task execution might block the main Thread
and crash the application with an ANR.
To simplify the asynchronous and concurrent executions, the RxJava
framework allows us to define a Scheduler
entity that defines the thread where a unit of work is executed.
The subscribeOn(Scheduler)
operator allows us to set the Scheduler that defines the thread on which the subscription has been made and the Observable will start to operate.
When...