英文:
How to add delay for some items in RxJava
问题
我想为以下目的实现一个 RxChain:
一个可观察的源正在发出两种类型的数据(S、E)。现在,我想要一个可观察的/可流式的对象,它将立即发出所有的 S,但是如果在此期间没有 S 发出,它应该在第一个 E 发出后的给定延迟(10 秒)后发出最新的 E。
英文:
I want to implement a RxChain for the following purpose:
An observable source is emitting two types of data (S, E). Now I want an observable/flowable which will emit all the S immediately but it should emit the latest E after a given delay (10 sec) from the first emission of E if no S comes in the meantime.
答案1
得分: 1
代替只有一个可观察的源同时发出S和E的做法,你可以将它们拆分为两个可观察的对象,在“E流”上添加一个10秒的throttleLast
,然后将它们合并在一起。
例如:
Observable<String> sStream = source.filter(x -> x.type == Types.S);
Observable<String> eStream = source.filter(x -> x.type == Types.E).throttleLast(10, TimeUnit.SECONDS);
Observable.merge(sStream, eStream).subscribe(...);
英文:
Instead of having 1 observable source emitting both S and E, you could instead split them into 2 observables, add a throttleLast
10s on the "E stream" and then merge them together
e.g.
Observable<String> sStream = source.filter(x -> x.type == Types.S);
Observable<String> eStream = source.filter(x -> x.type == Types.E).throttleLast(10, TimeUnit.SECONDS);
Observable.merge(sStream, eStream).subscribe(...);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论