Type Alias

dispatch_block_t

The prototype of blocks submitted to dispatch queues, which take no arguments and have no return value.

Declaration

typedef void (^dispatch_block_t)(void);

Discussion

The declaration of a block allocates storage on the stack. Therefore, this example demonstrates an invalid construct:

dispatch_block_t block;
 
if (x) {
    block = ^{printf("true\n"); };
} else {
    block = ^{printf("false\n"); };
}
block();  // unsafe!!

What is happening behind the scenes:

if (x) {
    struct Block __tmp_1 = ...;  // setup details
    block = &__tmp_1;
}  else {
    struct Block __tmp_2 = ...; // setup details
    block = &__tmp_2;
}

As the example demonstrates, the address of a stack variable is escaping the scope in which it is allocated.

See Also

Managing Units of Work

dispatch_function_t

The prototype of functions submitted to dispatch queues.

dispatch_block_create

Creates a new dispatch block on the heap using an existing block and the given flags.

dispatch_block_create_with_qos_class

Creates a new dispatch block on the heap from an existing block and the given flags, and assigns it the specified QoS class and relative priority.

dispatch_block_perform

Creates, synchronously executes, and releases a dispatch block from the specified block and flags.

dispatch_block_wait

Waits synchronously until execution of the specified dispatch block has completed or until the specified timeout has elapsed.

dispatch_block_notify

Schedules a notification block to be submitted to a queue when the execution of a specified dispatch block has completed.

dispatch_block_cancel

Asynchronously cancels the specified dispatch block.

dispatch_block_testcancel

Tests whether the given dispatch block has been canceled.