我有这样创建的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
            }
        })
    );
}

关于java - 将条件置于andThen方法Completable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44827071/

10-11 02:42