Schedulers


Schedulers : 

  1. A scheduler is an abstraction introduced by the RxJava library to schedule work at some point in time.
  2. The work happens in some asynchronous context.
  3. That context could be custom Threads, an event loop, Executors and so on.                                                                        

Using Subscribe On :


  1. This operator tells the source Observable which thread to use for emitting items to the Observer.
  2. subscribeOn → This method sets the thread where the observable is emitting. e.g. data coming from a network interface
  3. When you want to actually observe an observable, you subscribe to it. This determines where the original processing will happen. If subscribeOn is not called, then RxJava automatically uses the current thread
  4. This process is creating events on the main thread using the main scheduler. The AndroidSchedulers.mainThread()
  5. The subscribeOn operator allows you to provide a Scheduler to change what thread the observable creation code is called on.
  6. Order Does Not Matter - We can place it anywhere
Note: When multiple subscribeOns are used in succession, only the first one takes effect

Using observeOn : 


  1. This operator specifies the scheduler on which an observer will observe this Observable.
  2. By default, an Observable along with the operator chain will operate on the same thread on which its Subscribe method is called.
  3. observeOn → This method sets the thread where the observer is consuming data. e.g. we might want to update the UI or save data into the storage
  4. The observeOn() operator, however, will intercept emissions at that point in the Observable chain and switch them to a different Scheduler going forward.
  5. In opposition to subscribeOn, the operator observeOn changes the scheduler where the observation happens. Once an event is pushed by an Observable to all the subscribed observers, this operator will ensure that the event is handled by the correct scheduler.
  6. implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
  7. RxAndroid is an extremely small library whose entire purpose is to expose the Android main looper as a scheduler via the AndroidSchedulers.mainThread() static utility function.
  8. Order matters for observeOn




 Built in Schedulers: 


Android main scheduler - 

AndroidSchedulers.mainThread() sits on top of the main thread. This scheduler is used to process changes on the user interface and perform other high-priority tasks.


IO scheduler -

The scheduler returned by Schedulers.io() should be used whenever you’re doing work that’s IO bound. Specifically, if you’re making any network calls, accessing items from a database, or reading lines from a file, this is the scheduler for you. Under the hood, it’s backed by thread pool that will grow as needed, so make sure not to do strict computational work while using the IO scheduler.

Computation scheduler - 

If you do need to heavy computational work, like crunching large data sets or handling event loops, you can use the scheduler returned by Schedulers.computation().

In opposition to the IO scheduler, the computation scheduler will not spawn more threads as needed. Instead, the number of threads it works with is normally limited to the number of cores the CPU has.


Single threaded scheduler -

Sometimes, you need to work off the main thread but you also need guarantees that the work you’re doing is happening sequentially. 

However, if you have multiple distinct chains and you want to know that you’re continually adding new work to a queue, you can use the Schedulers.single scheduler.

The single scheduler is potentially the simplest of all the schedulers. It’s ultimately backed by one thread. That means that, whenever you queue up new work on that thread, it’s queued to the bottom so you know it happens after other work you’ve added before.

Trampoline scheduler -

Similar to the single scheduler, the scheduler returned by Schedulers.trampoline() always operates on a single thread. Unlike the single scheduler, that thread isn’t a background thread. Instead, it’s the main thread that created the trampoline scheduler. The trampoline scheduler can be very useful while writing unit tests.



Some important points: 

  • A Scheduler is an abstract context upon which RxJava executes work. In other words, Schedulers let you choose to do work on different threads.
  • You can use the subscribeOn operator to control on what thread your observable is created. That allows you to, for example, execute the actual networking portion of an API call off the main thread.
  • After using subscribeOn, you can use the observeOn operator to then choose a different thread to actually receive the emitted objects on. You’ll often use this operator to switch back to the main thread to update UI objects.
  • There are several built in schedulers for you to use. The IO scheduler is great for network and database calls, while the computation scheduler is good for event loops and computationally expensive code.
  • The RxAndroid library exposes another special scheduler you can use to emit items on the Android main thread.

Comments

Popular posts from this blog

Difference between Imperative Approach and Reactive Approach

The 10 Most Popular Coding Challenge Websites 2021

Java - Recursion