Browse Questions
  • CI/CD automates building, testing, and distributing your app on every code push.
  • Prefer composition (a type holds/uses other types to get behavior) when: - The relationship is "has-a" rather than "is-a" (a Car has an Engine, it isn't an Engine) - You need to mix behaviors from…
  • A Task is Swift's unit of async work.
  • TLS protects the connection, not the message — it ends at the first thing that terminates it, which may be a load balancer, a logging proxy, or an attacker's intercepting certificate on a…

Answer: A Task is Swift's unit of async work. Unlike threads, tasks are lightweight, managed by the Swift runtime, and can be suspended without blocking an underlying thread.

TaskThread
CostLightweightHeavy (~512KB stack)
Managed bySwift runtimeOS
SuspensionYes (non-blocking)No (blocks thread)
CancellationBuilt-inManual

Code Example:

// Creating a task
let task = Task {
    let data = try await fetchData()
    await MainActor.run { updateUI(with: data) }
}

// Cancelling it
task.cancel()

// Task inherits priority and actor context from where it's created
// Use Task.detached to explicitly break that inheritance
Task.detached(priority: .background) {
    await expensiveBackgroundWork()
}