1

I have something like:

private Single<List<Data>> getFirstApiResponse() {
    return Single.just(....)
         /////
         .subscribeOn(Schedulers.io())
         .observeOn(AndroidSchedulers.mainThread());
}

private Single<AnotherData> getSecondApiResponse() {
    return Single.just(....)
         /////
         .subscribeOn(Schedulers.io())
         .observeOn(AndroidSchedulers.mainThread());
}

public void execute() {
    //Here I need to run both observables one by one, and show result of each in View

   // Code exetuting both
   .subscribe(......);
}

How can I run two observables and subscribe on them in last method. In other words, I need to run method execute which will display in UI result of each Observables.

By the way, Observable not connected, they fetch different data (so I can run them asynchronous)

Mike
  • 79
  • 2
  • 9
  • Do you want it serial or parallel? – lelloman Oct 31 '18 at 20:47
  • No matter, but better if it will be faster as much as possible (so asynchronous better I think). This observables not connected at all, they receive (fetch) different data – Mike Oct 31 '18 at 20:49
  • I need to fecth data, and in UI show this data, for instance, first fetches `Person`, second fetches `Place`, in UI I need to show both – Mike Oct 31 '18 at 20:52

2 Answers2

1

One way to do that is with flatMap:

public void execute() {
    getFirstApiResponse()
            .flatMap(response1 -> {
                // getFirstApiResponse has completed
                // ...
                return getSecondApiResponse();
            })
            .subscribe(response2 -> {
                // getSecondApiResponse has completed
                // ...
            }, error -> {
                // One of the other operation has failed
            });
}
Myk Willis
  • 12,306
  • 4
  • 45
  • 62
  • I need to show data from first response and second response in UI, as I see, in your case I subscribe and show data only of second api response – Mike Oct 31 '18 at 20:47
  • One more thing, both Observables have different types, but I need to show both. For instance, first fetch Person, second Place, in UI I need to show both – Mike Oct 31 '18 at 20:48
  • If you can run the operations in parallel (I thought you specifically said in the question you needed them one after the other), you should use combineLatest() to run them at the same time. – Myk Willis Oct 31 '18 at 23:56
0

You could look into the zip operator as well, depending on your needs. The downside to this solution is you are forced to combine your responses into a pair or another suitable datastructure, which may not make sense for you.

public void execute() {
    Single.zip(getFirstApiResponse(), getSecondApiResponse(), 
                (first, second) -> {
                    //now you have both
                    return Pair.of(first, second);
                }).subscribe(pair -> {/**do stuff**/});
    }
RedDeckWins
  • 2,111
  • 14
  • 16
  • Yep, everything works great. Sorry, I'm newer in RxJava, can u explain what the difference in your approach and approach below (using flatMap), which is better? – Mike Oct 31 '18 at 21:30
  • The difference between zip (or combineLatest with Observables) and flatMap is that zip() will allow you to run the operations in parallel, processing them only when they are complete, whereas flatMap() allows you to run them one after the other. – Myk Willis Oct 31 '18 at 23:58