Skip to main content

Command Palette

Search for a command to run...

Combine: CombineLatest operator

Published
3 min read

Combine has a ton of operators. In this article, we are going to see about the CombineLatest operator.

CombineLatest

This operator combines different publishers. You could combine up to four publishers. The publishers can be of different value types. CombineLatest emits a tuple when any one of the publishers emits. This emitted tuple by the CombineLatest operator provides the latest value of all the publishers. One important thing to note here, CombineLatest operator emits only when all the publishers have emitted at least once.

We are considering an example of Name, Age validator below.

// 1
var subscriptions = Set<AnyCancellable>()
let namePublisher = PassthroughSubject<String, Never>()
let agePublisher = PassthroughSubject<Int, Never>()
// 2
namePublisher
  .combineLatest(agePublisher)
  .sink(receiveValue: { print("Name Publisher: \($0), Age Publisher: \($1)") })
  .store(in: &subscriptions)
// 3
namePublisher.send("a")
namePublisher.send("b")
agePublisher.send(20)
agePublisher.send(21)
namePublisher.send("c")
  1. We are creating namePublisher by using PassthroughSubject of type String, agePublisher by using PassthroughSubject of type Int. We had used a PassthroughSubject so that we could send values to the Publisher later and demonstrate the flow.
  2. To the chain of namePublisher, we are adding the agePublisher through the combineLatest operator. So that we could get the latest name and age. Then we are using the sink operator to print the values obtained from combineLatest and store the anyCancellable returned by the Publisher in the subscriptions set which we had created.
  3. Next we are sending the values to the name and age publisher.

You could see the console output like the below.

// 1 
Name Publisher: b, Age Publisher: 20

// 2
Name Publisher: b, Age Publisher: 21

//3
Name Publisher: c, Age Publisher: 21

When performing namePublisher.send("a") and namePublisher.send("b"), the print statement isn't executed because combineLatest operator hadn't emitted any values till now. This is due to the fact that the CombineLatest operator starts emitting only when all the publishers have emitted at least once. We hadn't sent agePublisher any value till now which resulted in not emitting values from the agePublisher.

  1. Next when agePublisher.send(20) is performed, Since the agePublisher had emitted the value of 20, the combineLatest operator also starts emitting the value and the print statement is executed as the personPublisher already has value. As you could see from the print statement, the latest value "b" in namePublisher is printed along with the now received value of 20 from agePublisher.
  2. Next when agePublisher.send(21) is performed, agePublisher triggers the combineLatest operator with its value of 21. The print statement is executed by getting the latest value "b" in namePublisher along with the now received value of 21 from agePublisher.
  3. Next when namePublisher.send("c") is performed, this time agePublisher takes a backseat and namePublisher triggers the combineLatest operator with its new value of C. The print statement is executed by the received value "c" in namePublisher along with the latest value 21 from agePublisher.

As you could see from the above CombineLatest operator emits only when both namePublisher and agePublisher have emitted at least once. This operator is useful for the scenario of getting to know, whenever any of the values changes across multiple publishers(like Validation Scenario).

That's a wrap for this article. Hope you got an idea about combineLatest operator. Will catch up with you in the next one. Happy Coding!!!