将条件放入 Completable 的 andThen 方法

Put condition to andThen method of Completable

我有一个 Completable 这样创建的:

public Completable doCalulations() {
    return Completable.fromCallable(() -> {
        //some calculations
    })
    .andThen(/*Here I want to sequentially execute another Completable*/);
}

在第一个 Completable 调用 onComplete 之后,我想根据某些条件顺序执行另一个 Completable

if (condition.check()) {
    return someCalculation(); //returns Completable
} else {
    return anotherCalculation(); //returns Completable
}

我该怎么做?

使用defer:

public Completable doCalulations() {
    return Completable.fromCallable(() -> {
        //some calculations
    })
    .andThen(
        Completable.defer(() -> {
            if (condition.check()) {
                return someCalculation(); //returns Completable
            } else {
                return anotherCalculation(); //returns Completable
            }
        })
    );
}