How can I get the current Task?

Hi everyone,

Is it possible to get the current task from within an async function?
I found withUnsafeCurrentTask(body:) and UnsafeCurrentTask. The documentation of UnsafeCurrentTask says:

[...] use the task property of UnsafeCurrentTask to access an instance of Task that you can store long-term and interact with outside of the closure body. [...]

However, it looks like this property is not available (Xcode 13 Beta 3) and I think it is also not mentioned in the Proposal.

Context: I want to get the current task during the execution of the closure that I pass to Task.init(priority:operation:) to be able to cancel the current task later.


1 Like

Given below is my understanding (I could be wrong):

You could use try Task.checkCancellation to check if the current task is cancelled, and I don't think you need to pass it around. If a parent task is cancelled all it's child tasks would be cancelled.


Thanks for help @SomuYadav, I have same issue Really appreciate for help.

The problem is that this Task I have does not have a parent Task. I use Task.checkCancellation() to check for cancellation but I want to get the Task to be able to cancel the Task in the first place.

The thing is that I call a method which does not use the new concurrency features from Swift and which may want to cancel the current task asynchronously. I want to give this method a Task so that it can cancel it. Because I need to call this method from inside the operation closure from Task.init(priority:operation:) I can't use the Task returned from the initializer.

1 Like
Terms of Service

Privacy Policy

Cookie Policy