Combine: CombineLatest operator
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")
- We are creating
namePublisherby usingPassthroughSubjectof typeString,agePublisherby usingPassthroughSubjectof typeInt. We had used aPassthroughSubjectso that we could send values to the Publisher later and demonstrate the flow. - To the chain of
namePublisher, we are adding theagePublisherthrough thecombineLatestoperator. So that we could get the latest name and age. Then we are using thesinkoperator to print the values obtained fromcombineLatestand store theanyCancellablereturned by the Publisher in the subscriptions set which we had created. - 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.
- Next when
agePublisher.send(20)is performed, Since theagePublisherhad emitted the value of 20, thecombineLatestoperator also starts emitting the value and the print statement is executed as thepersonPublisheralready has value. As you could see from the print statement, the latest value "b" innamePublisheris printed along with the now received value of 20 fromagePublisher. - Next when
agePublisher.send(21)is performed, agePublisher triggers thecombineLatestoperator with its value of 21. The print statement is executed by getting the latest value "b" innamePublisheralong with the now received value of 21 fromagePublisher. - Next when
namePublisher.send("c")is performed, this timeagePublishertakes a backseat andnamePublishertriggers the combineLatest operator with its new value of C. The print statement is executed by the received value "c" innamePublisheralong with the latest value 21 fromagePublisher.
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!!!