Dynamic delay value with repeatWhen()

后端 未结 5 1744

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

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 03:38

    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().

提交回复
热议问题