我坚持让下面的示例按预期工作,我尝试使用zip
和combineLatest
并在withLatestFrom
下方显示,但是没有给出预期的输出。
@Test
fun thereCanBeOnlyOne() {
val s1 = BehaviorSubject.create<Int>()
val s2 = BehaviorSubject.create<Int>()
s2.withLatestFrom<Int, Int, Int>(s1)
.subscribe { (a, b) ->
println("$a - $b")
}
s1.onNext(1)
s1.onNext(2)
s2.onNext(1)
s2.onNext(2)
s1.onNext(333)
s2.onNext(444)
}
我想要的是以下内容:
2 - 1
2 - 2
333 - 444
答案 0 :(得分:0)
经过一些实验后,我找到了解决方案:
@Test
fun thereCanBeOnlyOne() {
val s1 = BehaviorSubject.create<Int>()
val s2 = BehaviorSubject.create<Int>()
Observables.combineLatest(s1, s2)
.distinctUntilChanged { (_,b) -> b }
.subscribe { (a, b) ->
println("$a - $b")
}
s1.onNext(1)
s1.onNext(2)
s2.onNext(1)
s2.onNext(2)
s1.onNext(333)
s2.onNext(444)
}