Swift Academy · Lesson

PassthroughSubject and CurrentValueSubject

Using subjects as bridges between imperative code and Combine pipelines.

Lesson 3 of 412 steps

PassthroughSubject and CurrentValueSubject is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Subjects are both publishers and subscribers. They bridge imperative code to reactive Combine pipelines.

PassthroughSubject

```swift let subject = PassthroughSubject() subject.sink { print($0) }.store(in: &cancellables) subject.send("Hello") subject.send("World") // Hello, World ``` Late subscribers miss previously sent values.

CurrentValueSubject

```swift let subject = CurrentValueSubject(0) subject.sink { print($0) }.store(in: &cancellables) // prints 0 immediately subject.value = 1 // prints 1 subject.value = 2 // prints 2 ``` Stores and replays the current value to new subscribers.

PassthroughSubject vs CurrentValueSubject

• `PassthroughSubject` — no stored value; late subscribers see nothing • `CurrentValueSubject` — stores latest value; late subscribers get it immediately

Sending Completion

```swift subject.send(completion: .finished) // or: subject.send(completion: .failure(MyError.bad)) ``` After completion, no more values can be sent.

Using as Publisher

```swift let events: AnyPublisher = subject.eraseToAnyPublisher() // Expose only the Publisher interface, hide Subject ```

Bridge from Delegate

```swift class LocationManager: NSObject, CLLocationManagerDelegate { let locationSubject = PassthroughSubject() func locationManager(_ m: CLLocationManager, didUpdateLocations l: [CLLocation]) { locationSubject.send(l.last!) } } ```

Thread Safety

Subjects are not thread-safe by default. Send values from a consistent thread or use `receive(on:)` to switch.

Testing with Subjects

```swift func testSearch() { let subject = PassthroughSubject() var results: [String] = [] subject.sink { results.append($0) }.store(in: &cancellables) subject.send("swift"); subject.send("ios") XCTAssertEqual(results, ["swift","ios"]) } ```

Combine in MVVM

```swift class SearchViewModel: ObservableObject { let querySubject = CurrentValueSubject("") @Published var results: [String] = [] init() { querySubject .debounce(for: 0.3, scheduler: RunLoop.main) .flatMap { search($0) } .assign(to: &$results) } } ```

Quick Check

Which Subject replays its current value to new subscribers?

Recap

Key takeaways: • `PassthroughSubject` — no stored value, no replay • `CurrentValueSubject` — stores value, replays to late subscribers • `send(completion:)` ends the stream • `eraseToAnyPublisher()` hides the Subject interface • Subjects bridge imperative → reactive code Next: Networking with Combine.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “PassthroughSubject and CurrentValueSubject” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “PassthroughSubject and CurrentValueSubject”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “PassthroughSubject and CurrentValueSubject”?

Using subjects as bridges between imperative code and Combine pipelines. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “PassthroughSubject and CurrentValueSubject” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Publishers and Subscribers
  2. Operators: map, filter, combineLatest
  3. PassthroughSubject and CurrentValueSubject
  4. Networking with Combine and URLSession
← Back to Swift Academy