How task cancellation works with async/await
WHAT IT TESTS: Cooperative cancellation in Swift Concurrency. OUTLINE: Cancellation is a flag, not a kill; check Task.isCancelled or call checkCancellation, URLSession throws CancellationError automatically.
WHAT IT TESTS: Whether you understand Swift's cooperative cancellation model. ANSWER OUTLINE: Calling Task cancel() only sets an isCancelled flag; code must observe it. Built-in suspension points like URLSession.data and Task.sleep throw CancellationError when cancelled, so awaiting them propagates cancellation. Your own loops should poll Task.isCancelled or call Task.checkCancellation() and clean up. This matters for networking to stop wasted requests when a screen is dismissed, freeing connections and avoiding stale UI updates.
Read the original → interview
- #ios
- #swift
- #concurrency
- #async-await
- #cancellation
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.