0

I would like to create a Flowable to stream results received from an aynchronous REST API endpoint which sends results per block (it is necessary to send another request to get the remaining list of results using a header from the previous response). It don't want to use a backpressure strategy but rather wait for sending a subsequent HTTP request only when necessary (when requested in the Flowable).

The function Flowable.generate is really close to what I want but it seems to only work for synchronous calls. It there a solution like this one for asynchronous calls or do you have any advice to do this ?

Thanks

m.ostroverkhov
  • 1,910
  • 1
  • 15
  • 17

1 Answers1

1

I'm not sure if I understood you correctly, but I suppose you want to perform one request after another and you need some data from the previous request to perform next request.

Let's say, we have the following Flowable objects:

Flowable<String> first = Flowable.fromCallable(() -> {
  Thread.sleep(2000);
  return "I need to be executed first";
});

Flowable<String> second = Flowable.fromCallable(() -> "I need to be executed later");

First Flowable will be slower, but we want to execute second after first.

We can do that with concat(...) operator as follows:

Flowable.concat(first, second).subscribe(System.out::println);

According to RxJava documentation, concat operator emits the emissions from two or more Observables without interleaving them, so requests will be executed in the correct order.

You may also want to execute the second request depending on condition from the first request or with using some data from the first Flowable in the second Flowable.

You can achieve that with flatMap(...) operator as follows:

first.flatMap(
    s -> s.equals("I need to be executed first")
         ? second
         : Flowable.empty())
    .subscribe(System.out::println);

In this example, second Flowable will be emitted only, when the first one is exactly as we want to. If it's not, an empty Flowable will be emitted. If you want to use data from the first Flowable in the second one, you can create a new, custom Flowable inside the flatMap(...) operator instead of just returning second Flowable like that:

first.flatMap(
    s -> s.equals("I need to be executed first")
        ? Flowable.fromCallable(() -> "I'm using data from the first Flowable: ".concat(s))
        : Flowable.empty())
    .subscribe(System.out::println);

I hope, this answer will help you.

Piotr Wittchen
  • 3,853
  • 4
  • 26
  • 39