简体   繁体   English

RxAndroid:如何每分钟发出压缩的 observable?

[英]RxAndroid : How to emit zipped observable every minute?

Observable<List<Stop>> zippedObservable = Observable.zip(observableList, objects -> {
    List<Stop> stopList = Collections.emptyList();
    for (Object obj : objects) {
        stopList.add((Stop) obj);
    }
    return stopList;
});

I have the zippedObservable variable which was zipped by multiple observables.我有被多个 observable 压缩的 zippedObservable 变量。

disposable.add(zippedObservable.observeOn(AndroidSchedulers.mainThread())
    .subscribeWith(new DisposableObserver<List<Stop>>() {
    // onNext, onComplete, onError omitted
}));

This function emits the items (zipped stop list) successfully, but I'd like to emit these items every minute.此函数成功发出项目(压缩停止列表),但我想每分钟发出这些项目。 I assumed that interval operator would be perfect for this case, but I couldn't figure out how to mix both zip and interval functionalities.我认为interval运算符非常适合这种情况,但我无法弄清楚如何混合zipinterval功能。

This is what I tried这是我试过的

zippedObservale.interval() // cannot call interval operator here.
Observable.zip(...).interval() // cannot call interval operator here too.

I am looking for someone to explain how to mix these two operators so that I can emit the items every minute.我正在找人来解释如何混合这两个运算符,以便我可以每分钟发出这些项目。 Thank you.谢谢你。

interval is a static method that creates an Observable<Long> that emits a Long at a given period or interval. interval是一个静态方法,它创建一个Observable<Long> ,它在给定的时间或间隔发出一个Long

To achieve what you describe, you need to use one such Observable to pace your zipped Observable :为了实现你所描述的,你需要使用一个这样的Observable来调整你的压缩Observable

Observable<List<Stop>> zipped = ...;
Observable<Long> interval = Observable.interval(...);
Observable<List<Stop>> everyMinute = zipped.sample(interval);

In that case, it will simply emit at most one result of zipped every minute, dis-regarding whatever else zipped is emitting.在这种情况下,它最多只会每分钟发出一个zipped结果,而不管其他zipped I'm not sure that's what you want.我不确定那是你想要的。

If you want to simply re-emit the same value over and over, you might want to add a repeat() in between.如果您只想一遍又一遍地重新发送相同的值,您可能需要在两者之间添加一个repeat()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM