0Pricing
Swift Academy · Lesson

Single Source of Truth and Unidirectional Flow

Designing SwiftUI apps with one authoritative state model.

Single Source of Truth and Unidirectional Flow is a free Swift Academy lesson on CoddyKit — lesson 4 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

Unidirectional data flow means state flows down the view tree, and events flow up. One authoritative source of truth eliminates inconsistency.

The Principle

``` State → View → Event → State mutation → View re-renders ``` Views are pure functions of state. They don't decide what the state is — they only display it and emit events.

Model-driven UI

```swift struct AppState { var count = 0; var filter: Filter = .all } struct AppView: View { @State var state = AppState() var body: some View { CounterView(state: state, onIncrement: { state.count += 1 }) } } ```

Avoiding Duplicate State

```swift // ❌ Duplicate state — can drift out of sync: @State var isLoading = false @ObservedObject var viewModel: ViewModel // also has isLoading // ✓ Single source: @ObservedObject var viewModel: ViewModel // owns all state ```

Actions/Events Pattern

```swift enum CounterAction { case increment, decrement, reset } func reduce(state: inout AppState, action: CounterAction) { switch action { case .increment: state.count += 1 case .decrement: state.count -= 1 case .reset: state.count = 0 } } ```

TCA (The Composable Architecture)

The Composable Architecture (open-source) formalises this pattern in Swift with reducers, effects, and stores — similar to Redux in React.

Avoiding Two-Way Conflicts

```swift // ✓ Clear ownership: parent owns, child observes: struct Parent: View { @State private var text = "" var body: some View { Child(text: $text) } } struct Child: View { @Binding var text: String var body: some View { TextField("", text: $text) } } ```

Propagating Changes Up

Use closures (callbacks) to propagate events upward: ```swift struct Child: View { var onDelete: () -> Void var body: some View { Button("Delete", action: onDelete) } } ```

Testing Unidirectional Code

Pure state + pure reducer is trivially testable: ```swift var state = AppState() reduce(state: &state, action: .increment) XCTAssertEqual(state.count, 1) ```

Benefits

• Predictable: same state → same UI • Debuggable: log every action + state change • Testable: no UI required for logic tests • Time-travel debugging: replay state history

Quick Check

In unidirectional data flow, what triggers a state mutation?

Recap

Key takeaways: • State flows down; events flow up • One source of truth — no duplicate state • Use actions/reducers for testable mutation logic • Callbacks propagate events from child to parent • Pure state+reducer = easy unit tests Course complete! Next: Combine Framework.

Frequently asked questions

Is the “Single Source of Truth and Unidirectional Flow” lesson free?

Yes — the full text of “Single Source of Truth and Unidirectional Flow” 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 “Single Source of Truth and Unidirectional Flow”?

Designing SwiftUI apps with one authoritative state model. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Single Source of Truth and Unidirectional Flow” 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. @Binding: Two-Way Data Flow
  2. @ObservableObject and @StateObject
  3. @EnvironmentObject for Shared State
  4. Single Source of Truth and Unidirectional Flow
← Back to Swift Academy