Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Member-only story

Your Swift Task Was Cancelled. Why Is the Work Still Running?

--

Press enter or click to view image in full size

The bug is often not in cancel(). It is in the code that never checks whether cancellation happened.

👉 “You can read the full article here (no membership needed).”

You tap Cancel, dismiss a screen, or start a new search. You call task.cancel(). Yet the old work keeps running — or worse, updates the UI with a result that is already stale.

This is one of the easiest Swift Concurrency behaviours to misunderstand: cancellation is a request, not a forceful kill switch. Swift marks a task as cancelled, but the code doing the work must cooperate with that request.

This guide shows how that works, how a tiny try? can defeat cancellation, and how to design cancellable work for real iOS screens.

1. What Task.cancel() actually does

Apple documents three main effects of cancelling a task:

  • It marks the task as cancelled.
  • It runs active cancellation handlers.
  • It propagates cancellation to child tasks and task groups.

But cancellation is cooperative. Calling cancel() does not forcibly interrupt arbitrary synchronous code or automatically make every function throw. Code must check cancellation or call an…

--

--

Alok Upadhyay
Alok Upadhyay

Written by Alok Upadhyay

Turning Swift code into great iOS experiences. Writing about tools, tips, and tech for Apple developers.