RxJava Single.just() vs Single.fromCallable()?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)