Understanding Publishers in SwiftUI and Combine
Article Summary
Mike Pesate from Bumble breaks down Publishers in SwiftUI and Combine with real-world examples. If you're still confused about when to use CurrentValueSubject vs PassthroughSubject, this one's for you.
Publishers are the backbone of reactive programming in SwiftUI and Combine, but many developers struggle to understand when to use each type. Mike Pesate, a Senior iOS Developer at Bumble, demystifies the three most common publishers with practical code examples and real-world use cases.
Key Takeaways
- CurrentValueSubject stores values for immediate access by new subscribers
- PassthroughSubject emits values only to active subscribers, no storage
- Just sends one value then completes, perfect for cached data scenarios
- Publishers can be completed manually using send(completion: .finished)
- Real bonus: Using Just for image caching to avoid unnecessary network calls
Understanding the three core publisher types (CurrentValueSubject, PassthroughSubject, and Just) is essential for building reactive iOS apps as SwiftUI becomes the standard.
About This Article
Developers often don't know which Publisher protocol implementation to use. Apple's Combine framework has multiple ready-made publishers, but there's no clear guidance on how they actually differ.
Mike Pesate breaks down three publisher types with code examples. CurrentValueSubject stores values, PassthroughSubject streams without keeping them, and Just emits a single value before completing automatically.
When developers understand how publishers complete and use the send(completion: .finished) method correctly, they avoid losing values and manage subscriber lifecycles properly in reactive SwiftUI applications.