PassthroughSubject is a publisher that you control manually. Unlike Just or array.publisher which have their data baked in from the start, PassthroughSubject starts empty and you decide when to push values through it by calling .send(). Anyone subscribed to it receives those values instantly as they come in. This makes it perfect for real world scenarios like a search bar where you call subject.send(searchText) on every keystroke, or a login form where you push new values as the user types, and anything subscribed to that subject reacts to it immediately. When you're done sending values you call subject.send(completion: .finished) to tell subscribers the pipeline is done. So basically PassthroughSubject is Combines way of giving you a manual trigger that you can push data through whenever you want, making it the go to tool for user driven events and UI interactions.

Concept

Subjects allow external events to push values into a Combine pipeline.

In this example, the event source is a UITextField.

Every time the user types, the text is manually sent into a PassthroughSubject, which then forwards the value to a subscriber.

Conceptually:

UITextField Event → Subject → Subscriber

The subject acts as a bridge between UIKit events and Combine streams.


Subject Setup

A subject is created to publish text values.

private let textSubject = PassthroughSubject<String, Never>()

Type breakdown:

Output  → String
Failure → Never

This means the subject emits String values and never produces errors.


Subscriber

A subscriber is attached to the subject using sink.

textSubject
    .sink { value in
        print("Subscriber received:", value)
    }
    .store(in: &cancellables)

The subscriber receives every value emitted by the subject.