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.