What if your app could instantly stop doing things you no longer want, saving time and battery?
Why Cancellation handling in Swift? - Purpose & Use Cases
Imagine you start downloading a big file on your phone, but then you realize you don't need it anymore. Without a way to stop the download, your phone keeps working hard, wasting battery and data.
Manually checking if you want to stop a task means adding lots of extra code everywhere. It's easy to forget to check, causing the app to keep running slow or freeze. This makes your app feel unresponsive and frustrating.
Cancellation handling lets your app politely stop tasks when you don't need them anymore. It's like telling your phone, "Hey, stop what you're doing!" and it listens right away, saving time and resources.
if userWantsToCancel { stopTaskManually() } // Repeat checks in many places
task.cancel() // System handles stopping cleanly
It makes your apps faster, more responsive, and kinder to device resources by stopping work exactly when you want.
When you swipe to refresh a news app but then quickly scroll away, cancellation handling stops loading old articles so your phone focuses on what you want now.
Manual stopping is slow and error-prone.
Cancellation handling cleanly stops tasks on demand.
This improves app speed, responsiveness, and user happiness.