Home > Net >  How to add andThen() in a Completable.complete() programmatically in Rx / Reactive Java
How to add andThen() in a Completable.complete() programmatically in Rx / Reactive Java

Time:10-14

how do i make a chain of andThen() operator on an Completable ?

for example, the original code is this:

return Completable.complete()
        .andThen(processdata("01"))
        .andThen(processdata("02"))
        .andThen(processdata("03"))
        .andThen(processdata("04"))
        .andThen(processdata("05"))
        .andThen(processdata("06"))
        .andThen(processdata("07")); //working

it worked perfectly!

but i dont want a "static" defined value, and tried to convert the code above into this:

    Completable x = Completable.complete();
    String[] allID = {"01","02","09"}
    for (String Id : allID) {
        x.andThen(processdata(Id));
    }
    return x; //not working

and it is not woking, as if nothing happened

and then i realized that :

        Completable x = Completable.complete();
        x.andThen(processdata("01"));
        x.andThen(processdata("02"));
        x.andThen(processdata("03"));
        return x; //not working

is also not working ...

can anybody help how the proper way to chain a Completable in my case

CodePudding user response:

Try to chain the Completable this way because each call to andThen() is returning new instance of Completable.

Completable x = Completable.complete();
String[] allID = {"01","02","09"}
for (String Id : allID) {
  x = x.andThen(processdata(Id));
 }
return x;

CodePudding user response:

The idiomatic RxJava way would be to do it like this, so you don't have to keep reassigning Completable instances to the same reference:

return Observable.fromArray("01","02","09")
   .concatMapCompletable(id -> processData(id))
  • Related