我创建了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
}
我该怎么做?
答案 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
}
})
);
}