Recipes for Combining Observables in RxSwift

Several operators exist to combine multiple observables into one. This document shows the basics of the various combining operators and shows some more advanced recipes using them.

Combine Latest

The combineLatest operator is used whenever you have two or more observables emitting values, and you want access to the latest value emitted from each of them whenever one of them emits a new value. It can be used, for example, to combine a username and password to make a login request.

It also comes in handy when you have an array of Observables, and you want to convert them into an Observable that emits an array.

Sometimes, one of the Observables will emit several values before all of them have. The combineLatest operator will ignore those values until such time as all the constituents have emitted a value. If you don't want to miss out on any such values, then it's best to use startWith to provide defaults:

On occasion you need to use a value emitted by an Observable to create a new Observable, but you want to tie the value with the new Observable. For this, you can use just to lift the value back up into an Observable and combine it:

You could also do the above with an additional map:

Zip

Then there are times where you want to fire off a bunch of Observables, then wait until they all emit a value. If you know they will all emit no more than one value, you could use combineLatest. However, it's probably more expressive of intent if you use zip.

The zip operator also comes in handy if an Observable is emitting values too fast and you want to slow it down. Normally, from will emit all of the values in the array immediately upon subscription; by zipping it with an Observable that emits its values slowly, you can force from to slow down.

Merge

There are times when you have data coming from several possible sources and you want to treat it all the same; this is especially true with Void Observables which act as action triggers. For this merge is a great choice.

Another common use of merge is when you have Observables that perform different actions on the same (usually) piece of state. In these cases you don't exactly want to combine the emissions, rather you need them to come through one at a time, but you need to distinguish between them.

WithLatestFrom

Sometimes you want to combine observables, but you only want an event when one of them fires, not both. In that case, you use withLatestFrom(_:_:).

There is also a withLatestFrom(_:) that will give you the latest emission of the second observable and ignore the emission from the first observable. That version is great for triggers and is the one that is most commonly used.

Concat

Lastly, there are times when you want to fire off a bunch of Observables in sequence rather than all at once, but you still want to collect up all the results. The concat operator is good for that.

This will cause all the Observables in the networkRequests array to be subscribed to one at a time. The next one in the sequence won’t be subscribed to until the previous one completes. Make sure that the inner observables emit completion events or it will never finish.

Another great source to get a sence of how these operators work is in the Combining_Operators playground. Always be sure to check the official docs when you have any questions about an operator. Every one of them is well documented both as a doc comment and within a playground.

I hope this little tour of the combining operators helps out. If I got anything wrong, or your have better, or other, examples then by all means send me a DM on slack, twitter or reddit (danielt1263) or just leave a comment here.

I started programming as a hobby in the late ’70s and professionally in the late ’90s. I am the director of development at Haneke Design.