将条件放入和完成的方法

时间:2017-06-29 13:50:35

标签: java rx-java rx-java2

我创建了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
}

我该怎么做?

1 个答案:

答案 0 :(得分:3)

使用defer

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