RxJava Single.just()和Single.fromCallable()有什么区别?

41

我想知道有人能否解答这个问题,何时使用

Single.fromCallable( ()-> myObject )

不是

Single.just(myObject)

从文档中可以看到,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.
 */

以及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>
 */
4个回答

66
通常当您发出的不仅仅是一个对象,而是某些涉及重计算、I/O或状态的方法调用的结果时,您会注意到差异。

Single.just(x)会立即在当前线程中对x进行评估,然后您将获得x的结果,适用于所有订阅者。

Single.fromCallable(y)在订阅时在subscribeOn调度程序中调用y可调用对象,并单独为每个订阅者调用。


因此,例如,如果您想将I/O操作卸载到后台线程,则可以使用

Single.fromCallable(() -> someIoOperation()).
    subscribeOn(Schedulers.io()).
    observeOn(AndroidSchedulers.mainThread()).
    subscribe(value -> updateUi(value), error -> handleError(error));

如果这里使用Single.just()将无法生效,因为someIoOperation() 将在当前线程上执行。


58
在您提到的使用情况中,实际上没有太大区别。 现在想象一下,我们需要通过函数调用动态创建对象?
fun getTimeObject() {
    val timeInMillis = System.currentTimeMillis()
    return TimeObject(timeInMillis)
}

使用Single.just(getTimeObject()),生成的Single在有新订阅者时会发出相同的Long

然而,使用Single.fromcallable(()-> getTimeObject()),生成的Single会发出不同的Long,表示当前毫秒级别的时间戳,每当它有一个新的订阅者时就会执行fromCallable的lambda表达式,是一种惰性求值(Lazily)


4
我觉得这个例子很棒,因为它在很短的代码行中展示了这两个函数之间的显著差异。应该写进教科书里! - Samuel T. Chou

2

当你有一个类似的函数时,应该使用fromCallable()

MyObject myFunction() {
    // some login here
    return new MyObject();
}

然后您可以像这样从该函数创建Single

Single.fromCallable(() -> myFunction());

Single.just(myObject) 只是简单地发出您的对象,没有任何逻辑。

因此,当您想要发出特定项目时,无需使用 fromCallable()


2
在文档中,它们区分了两个时间点:组装时间运行时组装时间 应用各种中间操作准备数据流 运行时 当流主动发出项目时的状态
简单地说,Single.just()组装时间中被评估,而不是在主要过程完成后。 Single.defer() 和 Single.fromcallable()运行时中计算 Single 对象。
请在此处查看官方文档代码示例:链接

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接