What does it mean that an operator by default does not operate on any particular Scheduler?
Example: CombineLatest.
RxJava implements this operator as combineLatest. It may take between two and nine Observables (as well as the combining function) as parameters, or a single List of Observables (as well as the combining function). It does not by default operate on any particular Scheduler.
Android Scheduler — This Scheduler is provided by rxAndroid library. This is used to bring back the execution to the main thread so that UI modification can be made. This is usually used in observeOn method.
In the case of map operator, a project function is applied on each value on the source Observable and the same output is emitted as an Observable. A constant value is given as output along with the Observable every time the source Observable emits a value.
This operator transforms each item emitted by an Observable but instead of returning the modified item, it returns the Observable itself which can emit data again. In other words, they merge items emitted by multiple Observables and returns a single Observable.
ObserveOn specify the Scheduler on which an observer will observe this Observable. So basically SubscribeOn is mostly subscribed (executed) on a background thread ( you do not want to block the UI thread while waiting for the observable) and also in ObserveOn you want to observe the result on a main thread...
Operators do not require a particular Scheduler when they do not perform thread management operations.
That doesn't mean the code is not thread-safe. Thread safety is achieved by using scope containment, stack variables, volatile
values and Atomic
variables, with minimal use of synchronized
clauses.
Thread management is more sophisticated, and means moving operations or data between threads. In the code for such operators, you will see them creating Runnable
s, or TimerTask
s, which require knowing the scheduler.
It means that the operator follows this guideline stated in the Scheduler
docs --
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 its Subscribe method is called.
Everything it does will happen on the thread that subscribe was called on. Most operators work this way. Some cannot perform their work on same thread (like sample
), so need a second thread to perform the work. These operators generally have a default Scheduler
that they'll use for work, as well as a version that accepts a Scheduler
as a parameter to let you choose where they get the second thread to work on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With