Swift & Concurrency (50 Qs)•Part 1: Core Language, Memory & Value Types (1 - 10)
What are Tasks and TaskGroups in Swift Concurrency?
senior LevelCode ExampleswiftCore Language
In Simple Words:
Task: The basic unit of asynchronous execution. Inherits priority and actor context.- Structured Concurrency (
async let/withTaskGroup):async let: Runs multiple independent async calls in parallel and awaits them together.withTaskGroup: Runs a dynamic list of concurrent child tasks, automatically propagating cancellation if the parent task fails.
swift
// Parallel execution using async let:
async let avatar = downloadImage(url: avatarUrl)
async let stats = fetchStats(id: userId)
let profile = try await Profile(avatar: avatar, stats: stats)