1

Im stuck getting the following example working as expected, I have tried using zip and combineLatest and as show below withLatestFrom however non of them gives the expected output.

@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)
}

What I want is the following to print:

2 - 1

2 - 2

333 - 444

Community
  • 1
  • 1
user3139545
  • 6,882
  • 13
  • 44
  • 87

1 Answers1

1

After some more experimenting I found the solution in:

@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)
}
user3139545
  • 6,882
  • 13
  • 44
  • 87