Contents

name

Returns the human-readable name of the current task, if it was set during the tasks’ creation.

Declaration

static var name: String? { get }

Discussion

Tasks can be named during their creation, which can be helpful to identify unique tasks which may be created at same source locations, for example:

func process(items: [Int]) async {
  await withTaskGroup { group in
    for item in items {
      group.addTask(name: "process-\(item)") {
        await process(item)
      }
    }
  }
}

Task name availability

The task name is only available when running with a recent runtime (Swift 6.2+).

Task initializers which may accept a task name are more available than this property, for convenience purposes, in order to not have to set task names conditionally however their effect is runtime dependent, and is reflected in the availability of this property.