Schedulers

As described in the documentation, a scheduler is something that can schedule a unit of work to be executed now or later. In practice, it means that Schedulers control where the code will actually be executed and usually that means selecting some kind of specific thread.

Most often, Subscribers are used to executing long-running tasks on some background thread so that it wouldn't block the main computation or UI thread. This is especially relevant on Android when all long-running tasks must not be executed on MainThread.

Schedulers can be set with a simple .subscribeOn() call:

Observable.just("First item", "Second item")
.subscribeOn(Schedulers.io())
.subscribe();

There are only a few main Schedulers that are commonly used:

  • Schedulers.io()
  • Schedulers.computation()
  • Schedulers.newThread()
  • AndroidSchedulers.mainThread()

The AndroidSchedulers.mainThread() is only used on Android systems.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.191.86