If you want to introduce multithreading into your cascade of Observable operators, you can do so by instructing those operators (or particular Observables) to operate on particular Schedulers.
Schedulers give the opportunity to specify where and likely when to execute tasks related to the operation of an Observable chain.
Some ReactiveX Observable operators have variants that take a Scheduler as a parameter. These instruct the operator to do some or all of its work on a particular Scheduler.
By default, an Observable and the chain of operators that you apply to it will do its work, and will notify its observers, on the same thread on which itsSubscribemethod is called. TheSubscribeOnoperator changes this behavior by specifying a different Scheduler on which the Observable should operate. TheObserveOnoperator specifies a different Scheduler that the Observable will use to send notifications to its observers. As shown in this illustration, theSubscribeOnoperator designates which thread the Observable will begin operating on, no matter at what point in the chain of operators that operator is called.ObserveOn, on the other hand, affects the thread that the Observable will usebelowwhere that operator appears.For this reason, you may callObserveOnmultiple times at various points during the chain of Observable operators in order to change on which threads certain of those operators operate.
There are two methods through which we can introduce Schedulers into our chain of operations:
subscribeOn: It specify which Scheduler invokes the code contained in Observable.create().
observeOn:It allows control to which Scheduler executes the code in the downstream operators.
RxJava provides some general use Schedulers:
Schedulers.computation() : Used for CPU intensive tasks.
Schedulers.io(): Used for IO bound tasks.
Schedulers.from(Executor): Use with custom ExecutorService.
Schedulers.newThread():It always creates a new thread when a worker is needed. Since it’s not thread pooled and always creates a new thread instead of reusing one, this scheduler is not very useful.
This post will help you to find the useful extensions that you need in your android development. Checkout the Firebase Extentions | Getting start with the new extensions in Firebase | Firebase beta extensions Firebase Read more…
It’s an abstract class whose implementation is provided by the Android system. Context allows access to application-specific resources and classes, as well as calls for application-level operations such as launching activities, broadcasting and receiving intents, etc. Context is a handle Read more…
Background operation using RxJava Android What is Background operation? Background processing in Android refers to the execution of tasks in different threads than the Main Thread, also known as UI Thread, where views are inflated Read more…
0 Comments