Dynamic delay value with repeatWhen()

后端 未结 5 1750

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:55

    You could use the side effect operator doOnNext to update a delay variable and then use that in your repeatWhen

    int pollDelay = 5000;
    
    service.pollEndpoint()
    .doOnNext(pollResponse -> pollDelay=pollResponse.getDelay())
    .repeatWhen(observable -> observable.delay(pollDelay, TimeUnit.MILLISECONDS))
    .takeUntil(Blah::shouldStopPolling);
    

提交回复
热议问题