<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UnsafeCurrentTask",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sct"
  },
  "title" : "UnsafeCurrentTask"
}
-->

# UnsafeCurrentTask

An unsafe reference to the current task.

```
struct UnsafeCurrentTask
```

## Overview

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](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/).

## Topics

### Getting an Unsafe Reference to the Current Task

[`func withUnsafeCurrentTask<T>(body: (UnsafeCurrentTask?) throws -> T) rethrows -> T`](/documentation/Swift/withUnsafeCurrentTask(body:)-6gvhl)

Calls a closure with an unsafe reference to the current task.

[`func withUnsafeCurrentTask<T>(body: nonisolated(nonsending) (UnsafeCurrentTask?) async throws -> T) async rethrows -> T`](/documentation/Swift/withUnsafeCurrentTask(body:)-udlb)

Calls a closure with an unsafe reference to the current task.

### Instance Properties

[`var basePriority: TaskPriority`](/documentation/Swift/UnsafeCurrentTask/basePriority)

The current task’s base priority.

[`var hasActiveCancellationShield: Bool`](/documentation/Swift/UnsafeCurrentTask/hasActiveCancellationShield)

Checks if this task is executing in a scope with a task cancellation shield activated by the
`withTaskCancellationShield(operation:)-(()->Value)` function.

[`var isCancelled: Bool`](/documentation/Swift/UnsafeCurrentTask/isCancelled)

A Boolean value that indicates whether the current task was canceled.

[`var name: String?`](/documentation/Swift/UnsafeCurrentTask/name)

Return the task’s name, if it was set during its creation.

[`var priority: TaskPriority`](/documentation/Swift/UnsafeCurrentTask/priority)

The current task’s priority.

[`var unownedTaskExecutor: UnownedTaskExecutor?`](/documentation/Swift/UnsafeCurrentTask/unownedTaskExecutor)

The current [`TaskExecutor`](/documentation/Swift/TaskExecutor) preference, if this task has one configured.

### Instance Methods

[`func cancel()`](/documentation/Swift/UnsafeCurrentTask/cancel())

Cancel the current task.

[`func escalatePriority(to: TaskPriority)`](/documentation/Swift/UnsafeCurrentTask/escalatePriority(to:))

Escalate the task `priority` of the passed in task to the `newPriority`.

### Default Implementations

[Equatable Implementations](/documentation/Swift/UnsafeCurrentTask/Equatable-Implementations)

[Hashable Implementations](/documentation/Swift/UnsafeCurrentTask/Hashable-Implementations)

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`Hashable`](/documentation/Swift/Hashable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)