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
- The View sends Actions to a Store, which runs reduce(&state, action).
- Async work comes back as Effects that emit more Actions.
| Part | Owns | Looks like |
|---|---|---|
| State | Single value describing the screen | Struct, Equatable |
| Action | Everything that can happen | Enum |
| Reducer | Pure function: state + action → new state | static func reduce |
| Effect | Async work that emits actions | async closure, TCA Effect |
| Store | Runs 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 screen | Unidirectional. You need to know why state changed. |
|---|---|
| Complex shared state and a team that tests first | A library like TCA pays off, if everyone accepts the ramp-up and the dependency. |
| Simple forms and lists | MVVM. 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.
| MVVM | Unidirectional | |
|---|---|---|
| Where state changes | Any ViewModel method | Only the reducer |
| Side effects | Inline async calls | Returned as effects |
| Debugging | Breakpoints | Action log, replay |
| Ceremony | Low | High |
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.
Explain Unidirectional (TCA / Redux) in 30 seconds.
- Unidirectional flow keeps a screen's state in one value that only a reducer can change.
- The view sends actions, the reducer computes the next state, and side effects come back as new actions.
- Every change is traceable and replayable, and tests assert the exact state after each action.
- The cost is ceremony per event, so I use it for screens with many events and async flows, not simple forms.
Explain TCA's State, Action, Reducer, Store and Effects. What do you gain, and what does it cost?
Stateis a value type,Actionis an enum of everything that can happen, and theReducermutates state and returnsEffects, like network calls, that feed actions back in.- The
Storeruns the loop. - You get one consistent way to change state, exhaustive tests and features that compose.
- The cost is more setup than plain SwiftUI and a framework the whole team must learn.
- It works because state changes only inside the reducer, so every change is an action you can log, replay and test.
Async responses mutate the same screen state from three places, and bugs depend on arrival order. How do you make updates predictable?
- Make state read-only and change it only through actions like
.searchResponse(requestID, results). - A reducer applies them one at a time on the main actor, so transitions are serialized and logged.
- That alone doesn't fix arrival order: a slow old response can still land after a newer one.
- Tag requests with an id or cancel the previous effect, and have the reducer drop stale responses.
How do you test a feature where a tap fires a network request that later updates state?
- Use TCA's
TestStore. - Send the tap, assert the exact state change, then assert the effect's response with
await store.receive(\.response). - Inject the network client through
@Dependencyso each test swaps in a deterministic mock. - Unasserted actions fail the test, which catches surprise effects.
- The trade-off is longer tests, because every step needs an assertion.
Do you need a Redux-style framework to get one-way data flow in UIKit?
- No.
- You can enforce one-way flow with a library like TCA, get it through discipline, or let bindings produce it.
- Discipline, such as MVC plus a ViewState struct the controller renders, costs extra observation code.
- Bindings route input into the model and state back out automatically.
- Pick by how much enforcement your team needs.
- The Elm and Redux ideas are the same either way.