在固定间隔内定期轮询后端 API 一定次数 - Retrofit &RxJava [英] Polling to Backend API in regular interval for certain number of times in a regular interval - Retrofit & RxJava

查看:98
本文介绍了在固定间隔内定期轮询后端 API 一定次数 - Retrofit &RxJava的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在预定义的固定时间间隔内轮询后端调用一定次数.如果我在循环之间收到了预期的有效负载,我想退出循环并更新 UI,否则会终止轮询.

I am looking to poll the backend call for certain number of times for a predefined regular intervals. I would like to exit the loop if I have received an expected payload in between the loop and update the UI else terminate the polling.

以下是我在进行标准 http 调用时通常执行的代码.

Below is the code I normally do when I make standard http call.

//Response Model from backend API
public class ApplicationStatusResponse
{
public boolean isActive;
}

//Retrofit facade  
@POST(v1/api/applicationStatus)
Single<ApplicationStatusResponse> checkApplicationStatus(@Body ApplicationStatusRequest applicationRequest);


-----

DisposableSingleObserver<ApplicationStatusResponse> disposableSingleObserver = new DisposableSingleObserver<ApplicationStatusResponse>() {
    @Override
    public void onSuccess(ApplicationStatusResponse response) {
            // Update UI Here
    }

    @Override
    public void onError(Throwable e) {

    }
};

CompositeDisposable compositeDisposable = new CompositeDisposable();

// Following call works alaways works 
DisposableSingleObserver<ApplicationStatusResponse> disposable = originationRepo.checkApplicationStatus(applicationStatusRequest)
        .observeOn(schedulerProvider.mainThread())
        .subscribeWith(disposableSingleObserver);

compositeDisposable.add(disposable);

但是我在下面的代码中有点迷失了语法错误,从 Flowable.interval 调用时我无法使用相同的disposableSingleObserver 并且需要帮助我的用例,我需要更新UI定期状态,直到时间过去或状态处于活动状态,以先发生的为准,而且如果我收到 500 的 HTTP 状态代码,我也不会在终止轮询后重复,直到满足上述条件.

But I am kind of lost here in the following code with the syntax error and I am not able to use the same disposableSingleObserver when calling from the Flowable.interval and need help with my use case where I need to update the UI the status regularly until the time is elapsed or status is active which ever happens first and also I am not after terminating the polling if I received HTTP Status Code of 500 instead repeat until the mentioned conditions are met.

 //Help Needed here  when I need polling in regular interval - I am kind of the syntax error complain from Android Studio

int INITIAL_DELAY = 0;
int POLLING_INTERVAL = 1000;
int POLL_COUNT = 8;

disposable = Flowable
            .interval(INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS)
            .map(x -> originationRepo.checkApplicationStatus(applicationStatusRequest))
            .take(POLL_COUNT) ??
            // How can I receive the response payload and update the UI

compositeDisposable.add(disposable);

提前感谢您的帮助.

推荐答案

(继续使用 MyDogTom 的回答 你也可以通过抛出自定义错误/异常来短路"可观察对象)

(in continuation with MyDogTom's answer you could also "short-circuit" the observable by throwing a custom Error/Exception)

disposable = Flowable
        .interval(INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS)
        .map(x -> originationRepo.checkApplicationStatus(applicationStatusRequest)) // .flatMap (?)
        .take(POLL_COUNT) //YES
        .doOnNext() // update UI here
        .map(response -> {
           if(!response.checkCondition()) {
             throw new ShortCircuitException();
           }
             return response.data();
        })
        .onErrorResumeNext(throwable -> (throwable instanceof ShortCircuitException)
            ? Observable.empty()
            : Observable.error(throwable))

这篇关于在固定间隔内定期轮询后端 API 一定次数 - Retrofit &amp;RxJava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆