[英]How to make the map wait till the current index item is finished processing and then take the next item for processing using RxJava?
[英]Rxjava - How to get the current and the previous item?
如何使用运算符,以便我始终获得前一个值和当前值? 如果可能的话,我想避免在 pipe 之外创建 state。
- time ->
1 2 3 4
| | | |
Operations
| | |
(1,2) (2,3) (3,4)
请注意,除了第一个和最后一个值之外的每个值都必须出现两次,因此简单的缓冲区是行不通的。
我考虑过将skip
与merge
和buffer
结合起来,但合并似乎不能保证排序。
val s = PublishSubject.create<Int>()
s.mergeWith(s.skip(1)).buffer(2).subscribe{i -> print(i)}
s.onNext(1)
s.onNext(2)
s.onNext(3)
s.onNext(4)
output:
[1, 2][2, 3][3, 4]
val o = Observable.just(1,2,3,4)
o.mergeWith(o.skip(1)).buffer(2).subscribe{i -> print(i)}
output:
[1, 2][3, 4][2, 3][4]
(唯一的 4 很好,并且可以预期)
看起来你仍然可以使用缓冲区:
Observable.just(1, 2, 3, 4)
.buffer(2, 1)
.subscribe { println(it) }
// prints
// [1, 2]
// [2, 3]
// [3, 4]
// [4]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.