UnsafeCurrentTask

An unsafe reference to the current task.

iOS
13.0+
macOS
10.15+
tvOS
13.0+
watchOS
6.0+
struct UnsafeCurrentTask

To get an instance of UnsafeCurrentTask for the current task, call the withUnsafeCurrentTask(body:) method. Don’t store an unsafe task reference for use outside that method’s closure. Storing an unsafe reference 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 isn’t defined.

Only APIs on UnsafeCurrentTask that are also part of Task are safe to invoke from a task other than the task that this UnsafeCurrentTask instance refers to. Calling other APIs from another task is undefined behavior, breaks invariants in other parts of the program running on this task, and may lead to crashes or data loss.

For information about the language-level concurrency model that UnsafeCurrentTask is part of, see Concurrency in The Swift Programming Language.