Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

concurrency-swift

Concurrency

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Concurrency: the syntax, patterns, and safety habits you need before advancing in Swift.

async/await and actors model modern Swift concurrency—replacing GCD patterns in new code.

You will apply Concurrency in contexts like: Network fetches, background work, and responsive UI without blocking the main thread.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Swift concurrency uses async/await, Task, and actors for structured parallelism—compare with coroutines in Kotlin and goroutines in Go.

async/await basics

func fetchValue() async -> Int {
    try? await Task.sleep(nanoseconds: 50_000_000)
    return 42
}

Async functions suspend without blocking threads— the runtime resumes when work completes.

Tasks and actors

Task { } starts async work; actor types serialize access to mutable state—preventing data races at compile time where isolation rules apply.

Important interview questions and answers

  1. Q: async vs DispatchQueue?
    A: async/await is structured and compiler-checked; GCD remains under the hood but async APIs reduce callback pyramids.
  2. Q: MainActor?
    A: Annotates UI-bound code to run on the main thread—critical for SwiftUI updates locally.

Self-check

  1. What keyword marks a suspending function?
  2. Why use an actor instead of a class with a lock?

Tip: Use @MainActor for UI updates in real apps—async lessons use short Task.sleep demos.

Interview prep

async vs GCD?

async/await structured and compiler-checked; GCD under the hood.

MainActor?

Runs UI code on main thread—critical for SwiftUI.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • async await basics?
  • Actor isolation?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump