Class

DispatchWorkItem

The work you want to perform, encapsulated in a way that lets you attach a completion handle or execution dependencies.

Declaration

class DispatchWorkItem

Overview

A DispatchWorkItem encapsulates work to be performed on a dispatch queue or within a dispatch group. You can also use a work item as a DispatchSource event, registration, or cancellation handler.

Topics

Creating a Work Item

init(qos: DispatchQoS, flags: DispatchWorkItemFlags, block: () -> Void)

Creates a new dispatch work item from an existing block and assigns it the specified quality-of-service class.

struct DispatchWorkItemFlags

A set of behaviors for a work item, such as its quality-of-service class and whether to create a barrier or spawn a new detached thread.

Executing the Work Item

func perform()

Executes the work item's block synchronously on the current thread.

Adding a Completion Handler

func notify(queue: DispatchQueue, execute: DispatchWorkItem)

Schedules the execution of the specified work item after the completion of the current work item.

func notify(qos: DispatchQoS, flags: DispatchWorkItemFlags, queue: DispatchQueue, execute: () -> Void)

Schedules the execution of the specified work item, with the specified quality-of-service, after the completion of the current work item.

Waiting for the Completion of a Work Item

func wait()

Causes the caller to wait synchronously until the dispatch work item finishes executing.

func wait(timeout: DispatchTime) -> DispatchTimeoutResult

Causes the caller to wait synchronously until the dispatch work item finishes executing, or until the specified time elapses.

func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult

Causes the caller to wait synchronously until the dispatch work item finishes executing, or until the specified time elapses.

struct DispatchTime

A point in time relative to the default clock, with nanosecond precision.

struct DispatchWallTime

An absolute point in time according to the wall clock, with microsecond precision.

Canceling a Work Item

func cancel()

Cancels the current work item asynchronously.

var isCancelled: Bool

A Boolean value indicating whether the work item has been canceled.

See Also

Queues and Tasks

class DispatchQueue

An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread.

class DispatchGroup

A group of tasks that you monitor as a single unit.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software