I'm trying to send to the server the current time at the time the connection is established. I already know that this time may have some latency, regarding the health of the network. For this, I'm using retrofit(2) and rxjava(1), and I have some delay between retries and a retry count, something like this:
mRetrofit = new ApiClient().getObservableClient();
mService = mRetrofit.create(ApiObservableService.class);
mService.sendServerTimeNow(getCurrentTime())
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.retryWhen(«new RetryWithDelay(retries, delay))
.subscribe(new CustomSubscriber<ResponseBody>());
My problem is, every time the retry is made, the getCurrentTime() is not refreshed and its value is always the same on the time of subscription.
E.g.
Retry 1 - 1482399029862
Retry 2 - 1482399029862 //here should be changed to the new current time
Retry 3 - 1482399029862 //here should be changed to the new current time
I had the felling that retry would re-subscribe, and if this is true is not suppose to refresh the current time?
This is my getCurrentTime()
public long getCurrentTime(){
return System.currentTimeMillis();
}
How can I accomplish this refresh of current time?
Additionally, I already tried but with no success:
Observable.just(getCurrentTime())
.flatMap(new Func1<Long, Observable<ResponseBody>>() {
@Override
public Observable<ResponseBody> call(Long aLong) {
mService.sendServerTimeNow(aLong)
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.retryWhen(«new RetryWithDelay(retries, delay))
.subscribe(new CustomSubscriber<ResponseBody>());
You should first understand that
Observable.just(getCurrentTime())
executesgetCurrentTime()
when theObservable
sequence is assembled and doesn't make the call "magically" deferred:You can instead have
Observable.fromCallable(() -> getCurrentTime())
which will only callgetCurrentTime
for each incoming subscriber, including the one byretry
.