gpt4 book ai didi

java - RxJava 合并 debounced 和 not debounced observables

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:52:47 25 4
gpt4 key购买 nike

我有两个可观察对象:

Observable O(open): 包含一些内容的文件在 textview 中打开

Observable E(edit):在textview中编辑的文件内容

我想去抖动 E observable,并将其与 O observable 合并。

obs = Observable.merge(E.debounce(2000, TimeUnit.MILLISECONDS) , O)
.subscribe(content->System.out.println("new content: " + content))

问题是,如果 E 发出事件 E1 并且紧接着 O 发出 O1 事件,我们有输出:

new content: O1
new content: E1 // this output is rebundant (cuz we already have newer content O1)

这是正在发生的事情的图表:diagram

如何从 debounced observable 中去除这个过多的旧事件?

最佳答案

你可以试试

Observable.merge(O, O.switchMap(o -> E.debounce()))
.subscribe()

switchMap behaves much like flatMap, except that whenever a new item is emitted by the source Observable, it will unsubscribe to and stop mirroring the Observable that was generated from the previously-emitted item, and begin only mirroring the current one.

关于java - RxJava 合并 debounced 和 not debounced observables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39622791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com