Search code examples
iosswiftmergerx-swift

Is there an rx operator that merges streams, but one stream at a time?


I would like to merge two observable streams into one, just like you do with the .merge() operator. The reason I don't use that operator is because it runs the observables at the same time. I need an operator that merges two streams, but finish the first before it starts with the second one, so that the response would be something like: "stream1Value1", "stream1Value2", "stream2Value1", "stream2Value2".

Using the .merge() operator, the code looks lie this:

Observable.merge(self.getParameter(id: id).asObservable(), self.getSecondaryParameter(id: id).asObservable()).asSingle()

but gives me an output like this: "stream1Value1", "stream2Value1", "stream1Value2", "stream2Value2".

I have tried using Single.zip() but this gives me two parameters to work with ($0 and $1). Also I'm not sure if it finishes the first before starting with the second...


Solution

  • Learn more about all the combining operators in RxSwift in my article Recipes for Combining Observables in RxSwift.

    The operator you are looking for is called concat. There is a method as well as several static function overloads.

    Look in the "Concat.swift" file in the library for the various versions of the operator as well as their documentation.

    BTW, the zip operator, as well as combineLatest, also start both Observables at the same time.