I've noticed, that when a Task is cancelled while the work is suspended (on some for-await), awaiting is "interrupted" and work is continued, event if the stream is constantly sending values (not in a example for simplicity).
Here is a simple code that allow to recreate this behaviour.
import SwiftUI
import ConcurrencyExtras
struct QuestionTaskView: View {
@State var visible: Bool = false
var body: some View {
Button(action: {
visible.toggle()
}) {
Text("Toggle")
}
if visible {
Text("Task")
.task {
print("Task started")
let stream = AsyncStream<Int> { _ in } // stream that never ends
for await _ in stream {
}
print("Task ended")
}
}
}
}
#Preview {
QuestionTaskView()
}
After tapping twice on the Toggle button (to start and cancel the Task) this code prints in the console:
Task started
Task ended
I did not find any documentation about how cancelling a Task is dealing with suspension points. It is logical that a Task should allow to continue in order to clean up/finish, without this interruption Task would wait indefinitely. Where I can find more about which awaits are handled this way?