combineLatest(_:)
Subscribes to an additional publisher and publishes a tuple upon receiving output from either publisher.
Declaration
func combineLatest<P>(_ other: P) -> Publishers.CombineLatest<Self, P> where P : Publisher, Self.Failure == P.FailureParameters
- other:
Another publisher to combine with this one.
Return Value
A publisher that receives and combines elements from this and another publisher.
Discussion
Use combineLatest(_:) when you want the downstream subscriber to receive a tuple of the most-recent element from multiple publishers when any of them emit a value. To pair elements from multiple publishers, use zip(_:) instead. To receive just the most-recent element from multiple publishers rather than tuples, use merge(with:).
The combined publisher passes through any requests to all upstream publishers. However, it still obeys the demand-fulfilling rule of only sending the request amount downstream. If the demand isn’t unlimited, it drops values from upstream publishers. It implements this by using a buffer size of 1 for each upstream, and holds the most-recent value in each buffer.
In this example, PassthroughSubject pub1 and also pub2 emit values; as combineLatest(_:) receives input from either upstream publisher, it combines the latest value from each publisher into a tuple and publishes it.
let pub1 = PassthroughSubject<Int, Never>()
let pub2 = PassthroughSubject<Int, Never>()
cancellable = pub1
.combineLatest(pub2)
.sink { print("Result: \($0).") }
pub1.send(1)
pub1.send(2)
pub2.send(2)
pub1.send(3)
pub1.send(45)
pub2.send(22)
// Prints:
// Result: (2, 2). // pub1 latest = 2, pub2 latest = 2
// Result: (3, 2). // pub1 latest = 3, pub2 latest = 2
// Result: (45, 2). // pub1 latest = 45, pub2 latest = 2
// Result: (45, 22). // pub1 latest = 45, pub2 latest = 22When all upstream publishers finish, this publisher finishes. If an upstream publisher never publishes a value, this publisher never finishes.