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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…