Lidor Fadida’s Post

🛩️ Cancelling In-Flight Operations 🛩️ With Swift’s Modern Concurrency, canceling tasks mid-flight is both automatic and efficient, unlike the manual effort required with GCD. #Swift #iOSDevelopment #iOSProgramming #SwiftTips

  • text
Tema Nesterenko

iOS & Graphics Developer

1y

Does DispatchWorkItem not have automatic cancellation support?

Michael Long

Lead iOS Engineer | Writer | Open Source Author

1y

I usually choose to not clutter my Task code with [weak self] and self guards, especially if I'm checking for cancellation prior to performing additional work. Apple chose to mark the Task block with @_implicitSelfCapture for a reason. "The intent behind requiring self. when capturing self in an escaping closure is to warn the developer about potential reference cycles. The closure passed to Task is executed immediately, and the only reference to self is what occurs in the body. Therefore, the explicit self. isn't communicating useful information and should not be required." The task will always complete and captured self will always be released.

Paul Geurts

Freelance iOS Developer

1y

Thanks for the example 👍, still would prefer the old side-by-side comparison over these animated code examples, but that might just be me ;).

Mohit Kumar Dubey

Director - Generative AI & Machine Learning | LLMs, Agentic AI, LangGraph, LangChain, CrewAI, NLP, Computer Vision | AWS SageMaker, Google Vertex AI, Docker, Kubernetes, KubeFlow | Open to Global Opportunities

1y

Good to move to async await as it has cleaner code but your problem can be easily solved with dispatchWorkItem. A lot of developers actually lack this knowledge.

Ben Rudhart

App Engineer iOS & watchOS | Swift and Structured Concurrency Expert.

1y

Nice one! Thanks for sharing. Tipp: Instead of checking for `!Task.isCancelled` you could also simply wrap `try await Task.sleep` and the following code in a do { ... } catch { } block because `Task.sleep` is going to throw a cancellation error once it's cancelled.

Ahsan Murtaza

Senior Software Engineer - iOS at Coder Zone | Swift Developer

1y

just what I needed.

William FS.

Founder at @Bitcampo | Sênior Mobile

1y

How u create animated image? This awsome

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories