0

I recently learned to use merge where I combined 4 API requests into one output. (Reference: https://stackoverflow.com/q/51262421/1083093)

Restapi.class

/************/
    @GET("app/dashboard")
    Observable<CategoryHomeModel[]> getCategories(@HeaderMap Map<String, String> headers);

    @GET("app/wallet/balance")
    Observable<WalletBalance> getWalletBalance(@HeaderMap Map<String, String> headers);

    @GET("/app/swap/myrateswaps")
    Observable<SwapSettings> rateMySwap(@HeaderMap Map<String, String> headers);

    @GET("/app/getsettings")
    Observable<Settings> getSettings(@HeaderMap Map<String, String> headers);
    /************/

I have Four observables

Observable<CategoryHomeModel[]> categoriesObservable = retrofit
            .create(Restapi.class)
            .getCategories(prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    Observable<WalletBalance> walletObservable = retrofit
            .create(Restapi.class)
            .getWalletBalance(prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    Observable<Settings> settingsObservable = retrofit
            .create(Restapi.class)
            .getSettings(prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    Observable<SwapSettings> ratingsObservable = retrofit
            .create(Restapi.class)
            .rateMySwap(prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

Answer I found was useful using Merge function


Extending the Question above:

  • I have a similar question: How can I wait for first Observable to complete moving to second to third to fourth. Since I am using a variable obtained from first observable to pass it into second and so on till fourth observable

CONCAT - I searched is a good way to achieve this i think.

How to use CONCAT as i have used MERGE above ?

Devrath
  • 42,072
  • 54
  • 195
  • 297

1 Answers1

0

What you need is FLATMAP operator. It maps items emitted by the observable into new observables. It's convenient to use for Retrofit observables, where only one result item may be emitted by the observable. So we can transform observable item (request result) into observable representing next request in the sequence.

Referring to original question observable samples the code could look like this (assuming lamda syntax as in Java 8/RetroLambda):

categoriesObservable.flatMap(categoryHomeModel -> {
        /* Work on categoryHomeModel result; you can use it to configure next request */
        return walletObservable.flatMap(walletBalance -> {
            /* Work on walletBalance result; you can use it to configure next request */
            return settingsObservable.flatMap(settings -> {
                /* Work on settings result; you can use it to configure next request */
                return ratingsObservable;
            });
        });
    }).subscribe(swapSettings -> { /* Work on swapSettings; sequence is done */ });

This will make sure your requests are performed in sequence. Even if you use CONCAT, the results will be emitted to subscriber in sequence, but the actual requests may start in parallel, which sometimes is not expected. CONCAT/ZIP are good options if you have truly independent requests (e.g. querying different kinds of data), and it's ok (and even desirable) to run requests in parallel, when order of actual HTTP requests does not matter.

tpaczesny
  • 666
  • 8
  • 8