SwiftPumpkin

7 of 22 · UI patterns

Unidirectional (TCA / Redux)

Unidirectional data flow keeps a screen's state in one value that changes only in a reducer: the view sends actions, the reducer computes the next state, and side effects come back as more actions. It's the Elm and Redux model; TCA is the popular Swift implementation.

How it's wired

send(Action)StatereduceEffectActionViewRenders StateStoreHolds StateEffectAsync workReducer(State, Action) → State
calls or tellsreports backState changes in exactly one place. Side effects come back as actions, so every change is traceable.
  • The View sends Actions to a Store, which runs reduce(&state, action).
  • Async work comes back as Effects that emit more Actions.
PartOwnsLooks like
StateSingle value describing the screenStruct, Equatable
ActionEverything that can happenEnum
ReducerPure function: state + action → new statestatic func reduce
EffectAsync work that emits actionsasync closure, TCA Effect
StoreRuns reducer, publishes state@Observable class

Costs at scale

  • Ceremony per event: every tap is an action case, every async result another. Large features need scoping (child stores over a slice of state) or the root reducer becomes a god function.
  • Performance: with observable state (TCA 1.7+), views already track reads per property. The real cost is every action passing through each parent reducer, so keep high-frequency actions (scroll position, timers) away from the root.
  • The payoff is traceability: Equatable state and pure reducers make every change replayable, and exhaustive tests fail if you miss a state change.
  • Adopting a framework like TCA is a team-wide bet: onboarding, debugging through macros, and keeping up with its releases.

When I'd use it

Many events and async flows on one screenUnidirectional. You need to know why state changed.
Complex shared state and a team that tests firstA library like TCA pays off, if everyone accepts the ramp-up and the dependency.
Simple forms and listsMVVM. The ceremony doesn't pay back.

Compared

MVVM vs unidirectional

MVVM
ViewViewModel

State mutates in ViewModel methods.

Unidirectional
ViewStoreReducer

State mutates only in the reducer.

MVVMUnidirectional
Where state changesAny ViewModel methodOnly the reducer
Side effectsInline async callsReturned as effects
DebuggingBreakpointsAction log, replay
CeremonyLowHigh

Expert notes

  • Pick it for traceability, not fashion. If the team can't explain why a state changed, a reducer pays for itself.

Check yourself

Answer out loud before you reveal. Recall is what makes it stick.

30-second answer

Explain Unidirectional (TCA / Redux) in 30 seconds.

System designAsked directly

Explain TCA's State, Action, Reducer, Store and Effects. What do you gain, and what does it cost?

iOS codingAsked indirectly

Async responses mutate the same screen state from three places, and bugs depend on arrival order. How do you make updates predictable?

iOS codingAsked indirectly

How do you test a feature where a tap fires a network request that later updates state?

System designAsked indirectly

Do you need a Redux-style framework to get one-way data flow in UIKit?

Read it in the app's source