Skip to content

Combining Operators : Concat

Devrath edited this page Dec 29, 2023 · 1 revision

download

What is the Concat operator ?

  • The concat operator is used to concatenate the emissions of multiple Observables, one after the other, without interleaving them.
  • It waits for the one Observable to complete before subscribing to the next one.
  • We can also call it as synchronous.

Code

    private val observable1 = Observable.just(1,2,3)
    private val observable2 = Observable.just(4,5,6)
    private val observable3 = Observable.just(7,8,9)
    private val observable4 = Observable.just(10,11,12)
    
    private val concatObservable = Observable.concat(observable1,observable2,observable3,observable4)

    fun concat() {
        concatObservable.subscribeBy(
            onNext = { println("Result-> $it") },
            onError = { println("ErrorMessage-> ${it.localizedMessage}") },
            onComplete = { println("Emissions are complete") }
        ).addTo(subscription)
    }

Output

Result-> 1
Result-> 2
Result-> 3
Result-> 4
Result-> 5
Result-> 6
Result-> 7
Result-> 8
Result-> 9
Result-> 10
Result-> 11
Result-> 12
Emissions are complete
Clone this wiki locally