0Pricing
SwiftUI Academy · Lesson

Loading & Error States

Show spinners and handle failures gracefully.

Loading & Error States is a free SwiftUI 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 SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Every Fetch Has Phases

A real request moves through states: loading, success, and failure. Showing each one makes your app feel trustworthy. 🔄

Modeling State with an Enum

An enum captures each phase cleanly. The view then switches on it to decide exactly what to display.

enum Phase { case loading, loaded([User]), failed(String) }

Showing a Spinner

While loading, show a ProgressView. This native spinner reassures users that something is happening behind the scenes.

ProgressView("Loading...")

Switching on the Phase

Use a switch in your body to render a spinner, the data, or an error view based on the current phase.

switch phase {
case .loading: ProgressView()
case .failed(let msg): Text(msg)
case .loaded(let users): List(users)
}

Catching the Error

Wrap the fetch in do/catch and move to the failed phase on error, storing a friendly message for the screen.

catch { phase = .failed("Could not load") }

Friendly Error Messages

Avoid raw error text. Show a clear sentence the user understands, like "Check your connection and try again."

Offering a Retry

On failure, add a retry button that runs the fetch again. Letting users recover beats a dead-end error screen.

Button("Retry") { Task { await load() } }

The Empty State

Success with no data deserves its own empty view, like "No results yet", so the screen never looks broken.

Setting Loading First

Move to the loading phase before awaiting. That instantly shows the spinner so the screen never feels frozen.

phase = .loading
phase = .loaded(try await loadUsers())

Avoiding Flicker

For very fast responses, a tiny delay before hiding the spinner prevents a jarring flash of loading UI.

Wiring It to .task

Kick off the whole flow from .task, which sets loading, fetches, and lands on loaded or failed automatically.

.task { await load() }

Quick Check

Quick check on handling request phases.

Recap: States

You now model fetches as loading, loaded, and failed, show a spinner and friendly errors with retry, and drive it all from .task. 🎯

Frequently asked questions

Is the “Loading & Error States” lesson free?

Yes — the full text of “Loading & Error States” is free to read here on the web, and the SwiftUI 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 SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Loading & Error States”?

Show spinners and handle failures gracefully. You practise SwiftUI 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 SwiftUI Academy?

No prior experience is required. SwiftUI 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 “Loading & Error States” 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 SwiftUI Academy lesson?

Yes. Every SwiftUI 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. Decoding JSON with Codable
  2. Calling APIs with URLSession
  3. The .task Modifier
  4. Loading & Error States
← Back to SwiftUI Academy