🛩️ 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
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.
Thanks for the example 👍, still would prefer the old side-by-side comparison over these animated code examples, but that might just be me ;).
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.
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.
just what I needed.
How u create animated image? This awsome
iOS & Graphics Developer
1yDoes DispatchWorkItem not have automatic cancellation support?