What is combineLatest
in Combine?
The combineLatest
operator in Combine merges values from multiple publishers
and emits a new combined value whenever any publisher updates.
Think of it like a live news panel:
- Whenever a new headline or new expert opinion comes in, the panel updates.
- You always see the latest headline and the latest expert opinion together.
- If one source updates, the other keeps its most recent value.
combineLatest
emits a new value whenever any of its publishers emits.
🔗 Official Docs: Apple Documentation
How combineLatest
Works
- Starts when all publishers have emitted at least one value.
- Whenever any publisher emits a new value, the latest values from all publishers are combined and sent.
- Always uses the latest values available from each publisher.
Example: Live News Updates
import Combine
// 1. Create two publishers
let newsHeadlines = PassthroughSubject<String, Never>()
let expertOpinions = PassthroughSubject<String, Never>()
// 2. Combine the latest values
let cancellable = newsHeadlines
.combineLatest(expertOpinions)
.sink { headline, opinion in
print("📰 \(headline) | 💬 Expert: \(opinion)")
}
// 3. Send values (order matters)
newsHeadlines.send("Swift 6 Released!") // Won't print yet (needs both values)
expertOpinions.send("Game changer!") // ✅ Prints
newsHeadlines.send("Combine Improvements!") // ✅ Prints
expertOpinions.send("Developers will love it!") // ✅ Prints
Key Takeaways
- Starts after all publishers emit at least one value.
- Emits a new combined value every time any publisher updates.
- Always holds the latest value from each publisher.
- Useful for UI bindings, real-time data, and state management.