Please consider below code , How does swift knows which task is cancelled because
we are checking checkCancellation
using Task
's
public static func checkCancellation() throws
method and Task.isCancelled
?
when we are cancelling task1
how does i get cancellation status true in task1
closure but not in task2
even it is static property ?
I hope I am able to explain
let task1 = Task {
print("Task 1 started")
try Task.checkCancellation() // How does Task knows that cancelled task is this one not the task2 ?
print("Task 1 Finished")
}
let task2 = Task {
print("Task 2 started")
try Task.checkCancellation() // How does Task knows that cancelled task is this one not the task1 ?
print("Task 2 Finished")
}
Task {
do {
try task1.cancel()
} catch {
print("Task 1 cancelled")
}
}
Task {
do {
try task2.cancel()
} catch {
print("Task 2 cancelled")
}
}