RxJava: Looping Multiple Requests With Changing Parameters

38 Views Asked by At

Attempting to solve an issue in a flow that requires multiple callbacks and responses from the same endpoint.

For example, there is endpoint "/endpoint/action"

Each time I hit said endpoint I get a response, depending on that response I modify its response object and send that back to the server on the same "/endpoint/action", this whole process can repeat X number of times until the response on "/endpoint/action" signifies that the server is NOT asking for any more details.

For Example:

  • App -> Sends Request for "/endpoint/action"

  • Server -> Sends Response to "/endpoint/action" + Response code 200 + requesting data A

  • App -> Sends Request for "/endpoint/action" including data A

  • Server -> Sends Response to "/endpoint/action" + Response code 200 + requesting data B

  • App -> Sends Request for "/endpoint/action" including data B

  • ...

  • ...

  • ...

  • Server -> Sends Response to "/endpoint/action" + Response code 302

  • App -> Sees the 302 and breaks the loop

This loop could include any number of iterations.

I've attempted to use the "repeatUntil" functionality but that simply repeats the same request over and over instead of using the updated body between each request (as the requested data will change on every request as demonstrated above).

I've also looked at chaining Flatmaps, but this seems to require you to know the total number of requests that need to be made at the start, but this is all driven by the server, it may need to repeat 7 times, 8 times, 12 times, etc.

Using old fashion While loops have no context of the RxJava thread so it ends up calling the next iteration of the while loop before the request has finished so the condition for attempting another request has not yet been evaluated.

Any examples/suggestion would be appreciated!

0

There are 0 best solutions below