1

For example I have two network request:

Single<FirstResponse> firstRequest = firstCall()
Single<SecondResponse> secondRequest = secondCall()

Now, the Second should only be called when the first is successful, so I do something like:

firstRequest().flatmap( firstResponse -> secondRequest ).subscribe()

It works well when if both calls gets completed successfully but how about I dont want the first call to get called when it already returned success?

So what I want to achieve is that, when the firstRequest successfully completed and the second failed, I only want the first to be skipped and only call the second.

Currently the only thing I could think of doing is something like:

public firstResponse = null;

public Single<FirstResponse> getFirstRequest() {
    if (firstResponse!=null && firstResponse.isSuccess()) {
        return Single.just(firstResponse);
    } else {
        return firstRequest;
    }
}

public void doRequest() {
    getFirstRequest()
        .doOnSuccess( firstResponse -> this.firstReponse = firstResponse )
        .flatMap( firstResponse -> secondRequest)
        .subscribe()
}

I wonder if there is a better way to do this.

Thanks in advance.

Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107
  • Just to clarify, you need a `Completable` that when subscribed to, calls `firstCall()` and then call `secondCall()` on success. In case that `secondCall()` fails, `firstCall()` should not be called again, instead use a cached value from the successful response. Does `FirstResponse` need to be used for `secondCall()`? – Aarjav Sep 21 '18 at 16:50
  • FirstResponse will not be needed for secondCall().. Ojust need to ensure that firstCall() conpletes before secondCall() – Archie G. Quiñones Sep 23 '18 at 01:45

0 Answers0