Dispatch Group

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

Overview

Groups allow you to aggregate a set of tasks and synchronize behaviors on the group. You attach multiple blocks to a group and schedule them for asynchronous execution on the same queue or different queues. When all blocks finish executing, the group executes its completion handler. You can also wait synchronously for all blocks in the group to finish executing.

Topics

Creating a Dispatch Group

dispatch_group_create

Creates a new group to which you can assign block objects.

dispatch_group_t

A group of block objects submitted to a queue for asynchronous invocation.

Adding Work to the Group

dispatch_group_async

Schedules a block asynchronously for execution and simultaneously associates it with the specified dispatch group.

dispatch_group_async_f

Submits an application-defined function to a dispatch queue and associates it with the specified dispatch group.

Adding a Completion Handler

dispatch_group_notify

Schedules a block object to be submitted to a queue when a group of previously submitted block objects have completed.

dispatch_group_notify_f

Schedules an application-defined function to be submitted to a queue when a group of previously submitted block objects have completed.

Waiting for Tasks to Finish Executing

dispatch_group_wait

Waits synchronously for the previously submitted block objects to finish; returns if the blocks do not complete before the specified timeout period has elapsed.

Updating the Group Manually

dispatch_group_enter

Explicitly indicates that a block has entered the group.

dispatch_group_leave

Explicitly indicates that a block in the group finished executing.

See Also

Queues and Tasks

dispatch_get_main_queue

Returns the serial dispatch queue associated with the application’s main thread.

dispatch_get_global_queue

Returns a system-defined global concurrent queue with the specified quality-of-service class.

Dispatch Queue

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

Dispatch Work Item

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