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.
Task | Thread | |
|---|---|---|
| Cost | Lightweight | Heavy (~512KB stack) |
| Managed by | Swift runtime | OS |
| Suspension | Yes (non-blocking) | No (blocks thread) |
| Cancellation | Built-in | Manual |
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()
}