-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
87: Subjects: PassthroughSubject and CurrentValueSubject
I once worked with a developer who was building a real-time stock ticker app. He had everything wired up with Combine, but he hit a weird bug: when a user navigated to a specific stock's detail page, the price label would stay empty until the next price update arrived from the server, which could take thirty seconds. He was using a PassthroughSubject to broadcast price changes. The problem was that the detail view subscribed to the subject after the latest price had already been emitted. Since PassthroughSubject doesn't store anything, the new subscriber just sat there in the dark, waiting for an event that had already passed.
This is the fundamental divide between the two types of Subjects in Combine. While both allow you to manually "inject" values into a stream using the send() method, they behave very differently regarding memory and timing.
The Doorbell Approach: PassthroughSubject
Think of a PassthroughSubject like a doorbell. When someone rings it, the sound happens, and anyone listening at that exact moment hears it. If you step out of the house for a minute, you miss the ring entirely. There is no "recording" of the bell ringing; it's a pure event.
In your code, you use this for events that don't represent a persistent state. For example, a "Refresh" button tap or a "UserDidLogout" notification. You don't care if a subscriber missed the logout event from three minutes ago; you only care about the one happening now.
let notificationCenter = PassthroughSubject<String, Never>()
// Subscriber A joins early
notificationCenter.sink { print("Subscriber A received: \($0)") }
notificationCenter.send("Update 1") // Both hear it if B was here, but only A hears this.
// Subscriber B joins late
notificationCenter.sink { print("Subscriber B received: \($0)") }
notificationCenter.send("Update 2")
// Both A and B print "Update 2"
Notice that Subscriber B never saw "Update 1". It's gone forever. I usually reach for PassthroughSubject when I'm implementing a coordinator pattern or handling one-off triggers in a ViewModel.
The Light Switch Approach: CurrentValueSubject
Now, imagine a light switch. You don't need to be standing in the room when the switch is flipped to know whether the light is currently on or off. You can just walk in and look at the switch. That is exactly how CurrentValueSubject works.
A CurrentValueSubject requires an initial value and it remembers the most recent value it has emitted. When a new subscriber attaches, the subject immediately sends that current value to them. This solves the "blank label" problem my colleague had with the stock ticker.
let connectionStatus = CurrentValueSubject<String, Never>("Disconnected")
// Subscriber A joins
connectionStatus.sink { print("Subscriber A sees: \($0)") }
// Prints immediately: "Subscriber A sees: Disconnected"
connectionStatus.send("Connected")
// Subscriber B joins late
connectionStatus.sink { print("Subscriber B sees: \($0)") }
// Prints immediately: "Subscriber B sees: Connected"
One killer feature of CurrentValueSubject is that you can access the current state synchronously without a subscription. You can just call connectionStatus.value. This is incredibly useful when you need to check a state inside an if statement or a guard clause without setting up a whole pipeline. Just be careful not to rely on `.value` too heavily, or you're basically just using a fancy wrapper around a variable and losing the benefits of reactive programming.
📋 Practical Task
Exercise: Building a Connection Status Monitor
Create a NetworkMonitor class that simulates a network manager. Your task is to implement two different types of communication using Subjects:
- Connection State: Use a
CurrentValueSubjectto track whether the app isonlineoroffline. This should start asoffline. - Retry Trigger: Use a
PassthroughSubjectto signal when a user has manually tapped a "Retry" button.
To complete the exercise, write a small script that:
- Subscribes to the connection state and prints the current status.
- Subscribes to the retry trigger and prints "Attempting to reconnect..." whenever a signal is sent.
- Changes the connection state to
online. - Sends a retry signal.
- Creates a second subscriber to the connection state and verifies that it immediately receives the
onlinestatus without having to wait for a new change.
There are no comments for now.