mol
mol

Reputation: 2657

Put condition to andThen method of Completable

I have a Completable created like this:

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

After first Completable calls onComplete I want to execute sequentially another Completable based on some condition:

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

How can I do this?

Upvotes: 1

Views: 1410

Answers (1)

akarnokd
akarnokd

Reputation: 69997

Use defer:

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

Upvotes: 4

Related Questions