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.