0Pricing
Swift Academy · Lesson

Task and Task Cancellation

Creating structured tasks, checking cancellation and cooperative cancellation.

Task and Task Cancellation is a free Swift Academy lesson on CoddyKit — lesson 2 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 Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

`Task` is the fundamental unit of concurrency in Swift. It runs async work and supports structured cancellation for clean resource management.

Creating a Task

```swift Task { let user = try await fetchUser(id: 1) print(user.name) } ``` `Task` creates a new concurrent execution context.

Task Priority

```swift Task(priority: .userInitiated) { await loadContent() } Task(priority: .background) { await prefetch() } Task(priority: .utility) { await syncData() } ```

Task.cancel()

```swift var task: Task? func startLoading() { task = Task { await heavyWork() } } func stopLoading() { task?.cancel() } ```

Checking Cancellation

```swift func process() async throws { for item in items { try Task.checkCancellation() // throws if cancelled process(item) } } ```

Task.isCancelled

```swift func stream() async { while !Task.isCancelled { let data = await fetch() emit(data) } } ```

Detached Tasks

```swift Task.detached(priority: .background) { // NOT inheriting actor context or priority of the caller await heavyBackgroundWork() } ```

withTaskCancellationHandler

```swift await withTaskCancellationHandler { await doWork() } onCancel: { cleanup() } ```

Task Return Values

```swift let task = Task { 6 * 7 } let result = await task.value // 42 ```

Sleep

```swift try await Task.sleep(for: .seconds(2)) // Swift 5.7+ try await Task.sleep(nanoseconds: 1_000_000_000) // 1 second ```

Quick Check

Which method throws a `CancellationError` if the task has been cancelled?

Recap

Key takeaways: • `Task { }` — creates async work unit • `task.cancel()` + `Task.checkCancellation()` — cooperative cancellation • `Task.isCancelled` — poll-style check • `Task.detached` — no actor/priority inheritance • `Task.sleep(for:)` — non-blocking delay Next: async let.

Frequently asked questions

Is the “Task and Task Cancellation” lesson free?

Yes — the full text of “Task and Task Cancellation” is free to read here on the web, and the Swift 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 Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Task and Task Cancellation”?

Creating structured tasks, checking cancellation and cooperative cancellation. You practise Swift 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 Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Task and Task Cancellation” 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 Swift Academy lesson?

Yes. Every Swift 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. async/await Syntax and Calling Async Functions
  2. Task and Task Cancellation
  3. async let and Concurrent Child Tasks
  4. Actors: Protecting Shared Mutable State
← Back to Swift Academy