PassthroughSubject vs CurrentValueSubject

Combine Mid-level Swift 5.1+ iOS 13+ Reviewed By Alok Singh

Pick the fix

An auth service must tell screens whether the user is signed in, including screens created later. Which subject?

Options
Hint 1 of 1

What does a late subscriber need to see?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A CurrentValueSubject<Bool, Never>, exposed as AnyPublisher

Correct. New subscribers get the current state at once.

Why not the others?
  • B PassthroughSubject<Bool, Never>. Screens created later miss the last value until the next change.
  • C Just(true). Just emits one fixed value and finishes.
  • D Future. A Future emits a single value once; sign-in state changes over time.

30-second answer

Subjects are publishers you push values into with send, bridging imperative code into Combine. PassthroughSubject has no stored value, so late subscribers only see future values; it suits events like taps. CurrentValueSubject stores the latest value, replays it to new subscribers and exposes .value; it suits state.

Practise saying it · 30 s. No recording, just a timer

Compare the two subjects.

Say your answer out loud, then check which of these points you covered:

  • Push values with send
  • Passthrough for events
  • CurrentValue for state

Deep dive

@Published behaves like a CurrentValueSubject for one property, emitting in willSet.

Expose subjects as AnyPublisher so only the owner sends.

Sending a completion ends the subject for everyone.

Subjects aren't thread-safe for concurrent sends; send from one context.

Common mistakes

  • Using PassthroughSubject for state that late views need.
  • Sending from many threads.

Senior take & follow-ups

Seniors separate events from state and restrict who can send.

Follow-up questions

Think of your answer first, then open the hint.

  1. When does @Published emit?

    Show hint

    In willSet, before the property changes.

  2. Event or state?

    Show hint

    Passthrough for events, CurrentValue for state.

Suggest a fix if something here is wrong or out of date.