Bumble Sep 20, 2023

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

Critical Insight

Understanding the three core publisher types (CurrentValueSubject, PassthroughSubject, and Just) is essential for building reactive iOS apps as SwiftUI becomes the standard.

The article includes a quiz about what happens when you attach a new subscriber to a completed CurrentValueSubject that still holds a value.

About This Article

Problem

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.

Solution

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.

Impact

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.