0Pricing
Swift Academy · Lesson

PassthroughSubject and CurrentValueSubject

Using subjects as bridges between imperative code and Combine pipelines.

PassthroughSubject and CurrentValueSubject is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “PassthroughSubject and CurrentValueSubject” lesson free?

Yes — the full text of “PassthroughSubject and CurrentValueSubject” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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