使用 RXJava2/RXAndroid 2 和 Retrofit 进行轮询 [英] Polling using RXJava2 / RXAndroid 2 and Retrofit

查看:93
本文介绍了使用 RXJava2/RXAndroid 2 和 Retrofit 进行轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个轮询服务,它每 nDelay 秒调用一个 REST Api,并在数据发生更改时通知所有订阅者.现在我的代码有一个小问题,因为它总是向我的消费者返回一个值,即使数据没有改变.

i would like to implement a Pollingservice which calls a REST Api every nDelay Seconds and notify all subscribers if the data has been changed. Now i have a little problem with my code since it always returns a value to my Consumer, even if the data has not been changed.

private Observable<List<HueLight>> pollingLightsObservable = null;

public Observable<List<HueLight>> getPollingLightsObservable() {
        if (pollingLightsObservable == null) {
            pollingLightsObservable = Observable.fromCallable(
                    () -> LightManager
                            .getInstance(context)
                            .getLights()
                            .blockingSingle())
                    //      .distinctUntilChanged( (l1, l1) -> !l1.equals(l2) )
                            .repeatWhen(o -> o.concatMap(v -> Observable.timer(1, TimeUnit.SECONDS)));

        }
        return pollingLightsObservable;
 }

启用或使用 distinctUntilChanged 不会改变任何东西.放在repeatWhen之前还是之后都没有关系.

Enabling or using the distinctUntilChanged dont change anything. Doesnt matter if i put it before or after my repeatWhen.

由于我的 RetroFit Call 返回一个 Observable,我必须使用 blocksSingle().直接使用 Observable 会导致返回 "4, 8, 12, 16, .." 带有此示例的项目:

Since my RetroFit Call returns an Observable, i have to use blockingSingle(). Using the Observable directly it leads into a return of "4, 8, 12, 16, .." items with this sample:

LightManager.getInstance(context).getLights()
                            .repeatWhen(o -> o.concatMap(v -> Observable.timer(1, TimeUnit.SECONDS)))

目前我订阅了不同的课程/活动

Currently i subscribe from different classes/activites with

   this.lightChangeSubscriber = PollingManager
                .getInstance(getContext())
                .getPollingLightsObservable()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(hueLights -> {
                    {
                        Log.d(TAG, "Lights received successfully! Size=" + hueLights.size());
                    }
                });

我很想避免使用接口和计时器来创建轮询.你会推荐什么?

I would lovely avoid using interfaces and timer to create the polling. What would you recommend ?

推荐答案

使用一些自定义过滤器怎么样?

what about using some custom filter?

public class FilterDuplicateHueConfig implements Predicate<HueConfig> {

   private HueConfig lastVal;
   @Override 
   public boolean test(HueConfig newVal) {
      if(lastVal == null) {
         lastVal = newVal;
         return true;
      }
      ... compare here the two values and return true/false appropriately...
   }
}

这篇关于使用 RXJava2/RXAndroid 2 和 Retrofit 进行轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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