1

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

herman s
  • 11
  • 3

2 Answers2

2

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));
dano
  • 91,354
  • 19
  • 222
  • 219
  • thanks for the suggestion, but it is still not woking the processData(id) is not being executed ..., i forgot to mention that the processData(id) itself returning a Completable.complete(), probably somethings is still missing in the syntax. – herman s Oct 14 '22 at 04:45
  • @hermans The code in my answer definitely works (aside from a missed semicolon, which I've added). I think I'd need to see what you tried to tell you what's wrong. – dano Oct 14 '22 at 14:34
  • hello @dano, i was also wondering why the code isnt working ... all i wanted to do is just reworking someone's code (first code in the question). in which involving a loop of a lot of processData(id) with different id arguments .... i did know that processData(id) itself runs a bunch other process and at the end returns a Completable.complete(). ... i dont yet know exactly why your answer wont execute, is it somehow because not being "completed()" so it wont execute? – herman s Oct 14 '22 at 17:11
  • @hermans If `processData()` just does a bunch of stuff then returns `Completable.complete()`, it should not return a Completable at all, it should just return void. I suspect what you're seeing is because the `processData()` function is passed directly to `andThen()`, not in passed in a callback, like with `concatMapCompletable()`. Because the implementation of `processData()` is just immediately doing some work, that work is running immediately when the code hits the `andThen()` call, rather than being delayed until there is a subscription made downstream. (Continued) – dano Oct 14 '22 at 19:23
  • There is probably a `susbscribe` call missing from whoever calls the method that you're trying to refactor. The `fromArray()` method won't emit anything until it's subscribed to, which means the callback in `concatMapCompletable()` never runs. When you use `andThen()`, `processData()` gets called before the subscription ever happens, so it looks like things are working. – dano Oct 14 '22 at 19:25
  • ach yes you probably right, although i am must do some refactoring of the original code, i dont know exactly what the processData(id) are doing, i know at least it did some data parsing, calculation, networking process, and also bunch other long complex process, a bit of spaghetti code. The method that calls this processData(id) itself is just small subprocess in this whole rxjava framework used in this apps. nevertheless i thank you for the answer, i will definitely used it on my future works – herman s Oct 18 '22 at 04:20
0

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;
artiomi
  • 383
  • 2
  • 7