Help with AsyncStream termination task termination #1906
-
Trying to find a way to terminate an AsyncStream when it is no longer needed. I have a TCA application that uses enum-drven navigation that operates as a big state machine pulling real-time data from a vehicle using an AsyncStream in a dependancy. Think of the download example in the Case Studies providing progress updates forever so there is no call to
I found this code strange since
You can see that the original ( Any suggestions on programmatically killing off this stream from the TCA reducer? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I believe the problem is you're checking for your task cancellation incorrectly, and so the while loop runs indefinitely. You could instead check
You can reproduce this with the following playground:
|
Beta Was this translation helpful? Give feedback.
I believe the problem is you're checking for your task cancellation incorrectly, and so the while loop runs indefinitely.
Task.checkCancellation()
is meant to throw an error, and you're swallowing it withtry?
so its unable to halt the execution.You could instead check
Task.isCancelled
andbreak
if it's true. Replace your linetry? Task.checkCancellation()
with the following:You can reproduce this with the following playground: