withUnsafeCurrentTask(body:)
Calls a closure with an unsafe reference to the current task.
Declaration
@abi(nonisolated(nonsending) func withUnsafeCurrentTaskNonsending<T>(body: nonisolated(nonsending) (UnsafeCurrentTask?) async throws -> T) async rethrows -> T) nonisolated(nonsending) func withUnsafeCurrentTask<T>(body: nonisolated(nonsending) (UnsafeCurrentTask?) async throws -> T) async rethrows -> TParameters
- body:
A closure that takes an
UnsafeCurrentTaskparameter. Ifbodyhas a return value, that value is also used as the return value for thewithUnsafeCurrentTask(body:)function.
Return Value
The return value, if any, of the body closure.
Discussion
If you call this function from the body of an asynchronous function, the unsafe task handle passed to the closure is always non-nil because an asynchronous function always runs in the context of a task.
The operation is guaranteed to execute on the caller’s isolation.
Storing an unsafe reference to a task doesn’t affect the task’s actual life cycle, and the behavior of accessing an unsafe task reference outside of the withUnsafeCurrentTask(body:) method’s closure is unsafe and undefined behavior. There’s no safe way to retrieve a reference to the current task and save it for long-term use. To query the current task without saving a reference to it, use properties like currentPriority. If you need to store a reference to a task, create an unstructured task using Task.detached(priority:operation:) instead.