Right now I\'m implementing some polling logic with RxJava. I\'m supposed to poll an endpoint a number of times until it tells me to stop. Additionally, each response come
This is the solution I ended up using:
public static Observable createPollObservable(RetrofitService service, PollResponse response) {
return Blah::shouldStopPolling(response)
? Observable.empty()
: service
.pollEndpoint()
.delaySubscription(getPollDelay(response), TimeUnit.MILLISECONDS)
.concatMap(response1 -> createPollObservable(service, response1)
.startWith(response1)
.takeUntil(Blah::shouldStopPolling)
);
}
It instead uses recursion to always have the latest PollResponse object and also switches to delaySubscription()
rather than repeatWhen()
.