Nikita Lazarev-Zubov
1 min readAug 15, 2024

--

Are you sure you understand task cancellation correctly?

First of all, I don't think removing items from a set of cancellables cancels the corresponding tasks. It's just a Set, a collection of items, and removing items doesn't have any side effects on the items. You need to actually call the cancel() method on the tasks.

Secondly, cancelling a task doesn't magically remove that task from a queue, it's just sets a boolean flag that you need to check manually inside the task to determine whether the task is cancelled or you can proceed with the work (see https://www.hackingwithswift.com/quick-start/concurrency/how-to-cancel-a-task, for instance).

Lastly and closer to the topic, in any case cancelling mechanism has nowt to do with the block of code captured by the task. Cancelled or not, the block of code stays in memory and continues retaining everything it captured by strong references until the block is executed.

--

--

Nikita Lazarev-Zubov
Nikita Lazarev-Zubov

Written by Nikita Lazarev-Zubov

Swift and general programming topics, Agile software development, soft skills

Responses (1)