简体   繁体   中英

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.

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.

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.

To achieve what you describe, you need to use one such Observable to pace your zipped 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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