我有两个可观察对象:
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)
这是正在发生的事情的图表:
如何从 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/