0

I am using RxJava combineLatest operator.

According to the documentation, when subscribing, the first combine emit event will occur only after both observables emits there values, and from this point, it will emit on every emit of each of the Observables.

How can I determine that the first observable emit event has been occurred and it now waits for the second one..

Moti Bartov
  • 3,454
  • 33
  • 42
  • 1
    By logging the observables when they emit their event? – m0skit0 Oct 25 '18 at 09:10
  • It is hard as the observables are from different scope of my app. I don't subscribe the observables at their creation scope. Only in the combineLatest place. They are behaviour subjects observables. – Moti Bartov Oct 25 '18 at 09:23
  • 1
    Right, in that case, you want to hook up `doOnEac`h to each of the `BehaviourSubjects` that you are combining with `combineLatest`. That way, you're logs will be printed for each of the combined subjects (rather than trying to hookup `doOnEach` to the downstream of `combineLatest`. – Thomas Cook Oct 25 '18 at 09:36
  • Thanks, I can use both approaches! – Moti Bartov Oct 25 '18 at 09:43

1 Answers1

2

You can use doOnEach for Observables and Flowables or doOnEvent for Singles, Completables and Maybes. What those operations do is execute some code each time an item is emitted from the source before passing the signal down stream. It's very useful for "injecting" debug/logging code into an RX stream.

See: https://proandroiddev.com/briefly-about-rxjava-logging-20308b013e6d

EDIT:

val a = BehaviorSubject.create<Int>().apply {
    onNext(1)
}.doOnEach { EventReporter.d(TAG, it.value?.toString() ?: "") }

val b = BehaviorSubject.create<Int>().apply {
    onNext(2)
}.doOnEach { EventReporter.d(TAG, it.value?.toString() ?: "") }

Observable.combineLatest(listOf(a, b), { args: Collection<Int> -> args}
Thomas Cook
  • 4,371
  • 2
  • 25
  • 42