I wondered if someone can shed some light on this question, when to use
Single.fromCallable( ()-> myObject )
instead of
Single.just(myObject)
from the documentation, Single.fromCallable()
:
/** * Returns a {@link Single} that invokes passed function and emits its result for each new SingleObserver that subscribes. * <p> * Allows you to defer execution of passed function until SingleObserver subscribes to the {@link Single}. * It makes passed function "lazy". * Result of the function invocation will be emitted by the {@link Single}. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code fromCallable} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param callable * function which execution should be deferred, it will be invoked when SingleObserver will subscribe to the {@link Single}. * @param <T> * the type of the item emitted by the {@link Single}. * @return a {@link Single} whose {@link SingleObserver}s' subscriptions trigger an invocation of the given function. */
and the documentation for Single.just()
:
/** * Returns a {@code Single} that emits a specified item. * <p> * <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.just.png" alt=""> * <p> * To convert any object into a {@code Single} that emits that object, pass that object into the * {@code just} method. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code just} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param item * the item to emit * @param <T> * the type of that item * @return a {@code Single} that emits {@code item} * @see <a href="http://reactivex.io/documentation/operators/just.html">ReactiveX operators documentation: Just</a> */
from the documentation, Single.fromCallable() : /** * Returns a {@link Single} that invokes passed function and emits its result for each new SingleObserver that subscribes. * <p> * Allows you to defer execution of passed function until SingleObserver subscribes to the {@link Single}.
Single is an Observable which only emits one item or throws an error. Single emits only one value and applying some of the operator makes no sense. Like we don't want to take value and collect it to a list.
RxJava (and its derivatives like RxGroovy & RxScala) has developed an Observable variant called “Single.” A Single is something like an Observable, but instead of emitting a series of values — anywhere from none at all to an infinite number — it always either emits one value or an error notification.
RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.
Usually you will notice the difference when the thing you're emitting is not just an object but actually a result of some method calls that involve either heavy computation, I/O, or state.
Single.just(x)
evaluates the x
immediately in the current thread and then you're left with whatever was the result of x
, for all subscribers.
Single.fromCallable(y)
invokes the y
callable in the subscribeOn
scheduler at the time of subscription and separately for each subscriber.
So for example, if you wanted to offload an I/O operation to a background thread, you'd use
Single.fromCallable(() -> someIoOperation()). subscribeOn(Schedulers.io()). observeOn(AndroidSchedulers.mainThread()). subscribe(value -> updateUi(value), error -> handleError(error));
Having Single.just()
here would not work since someIoOperation()
would be executed on the current thread.
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