block.h
IntroductionUse the links in the table of contents to the left to access the documentation. GroupsDispatch block objectsGroup members:
Functions
dispatch_block_cancelAsynchronously cancel the specified dispatch block object. ( macos( 10.10), ios( 8.0)) void dispatch_block_cancel( dispatch_block_t block); ParametersDiscussionCancellation causes any future execution of the dispatch block object to return immediately, but does not affect any execution of the block object that is already in progress. Release of any resources associated with the block object will be delayed until execution of the block object is next attempted (or any execution already in progress completes). NOTE: care needs to be taken to ensure that a block object that may be canceled does not capture any resources that require execution of the block body in order to be released (e.g. memory allocated with malloc(3) that the block body calls free(3) on). Such resources will be leaked if the block body is never executed due to cancellation. dispatch_block_createCreate a new dispatch block object on the heap from an existing block and the given flags. ( macos( 10.10), ios( 8.0)) DISPATCH_NONNULL2 DISPATCH_RETURNS_RETAINED_BLOCK dispatch_block_t dispatch_block_create( dispatch_block_flags_t flags, dispatch_block_t block); ParametersReturn ValueThe newly created dispatch block object, or NULL. When not building with Objective-C ARC, must be released with a -[release] message or the Block_release() function. DiscussionThe provided block is Block_copy'ed to the heap and retained by the newly created dispatch block object. The returned dispatch block object is intended to be submitted to a dispatch queue with dispatch_async() and related functions, but may also be invoked directly. Both operations can be performed an arbitrary number of times but only the first completed execution of a dispatch block object can be waited on with dispatch_block_wait() or observed with dispatch_block_notify(). If the returned dispatch block object is submitted to a dispatch queue, the submitted block instance will be associated with the QOS class current at the time of submission, unless one of the following flags assigned a specific QOS class (or no QOS class) at the time of block creation: - DISPATCH_BLOCK_ASSIGN_CURRENT - DISPATCH_BLOCK_NO_QOS_CLASS - DISPATCH_BLOCK_DETACHED The QOS class the block object will be executed with also depends on the QOS class assigned to the queue and which of the following flags was specified or defaulted to: - DISPATCH_BLOCK_INHERIT_QOS_CLASS (default for asynchronous execution) - DISPATCH_BLOCK_ENFORCE_QOS_CLASS (default for synchronous execution) See description of dispatch_block_flags_t for details. If the returned dispatch block object is submitted directly to a serial queue and is configured to execute with a specific QOS class, the system will make a best effort to apply the necessary QOS overrides to ensure that blocks submitted earlier to the serial queue are executed at that same QOS class or higher. dispatch_block_create_with_qos_classCreate a new dispatch block object on the heap from an existing block and the given flags, and assign it the specified QOS class and relative priority. ( macos( 10.10), ios( 8.0)) DISPATCH_NONNULL4 DISPATCH_RETURNS_RETAINED_BLOCK dispatch_block_t dispatch_block_create_with_qos_class( dispatch_block_flags_t flags, dispatch_qos_class_t qos_class, int relative_priority, dispatch_block_t block); Parameters
Return ValueThe newly created dispatch block object, or NULL. When not building with Objective-C ARC, must be released with a -[release] message or the Block_release() function. DiscussionThe provided block is Block_copy'ed to the heap and retained by the newly created dispatch block object. The returned dispatch block object is intended to be submitted to a dispatch queue with dispatch_async() and related functions, but may also be invoked directly. Both operations can be performed an arbitrary number of times but only the first completed execution of a dispatch block object can be waited on with dispatch_block_wait() or observed with dispatch_block_notify(). If invoked directly, the returned dispatch block object will be executed with the assigned QOS class as long as that does not result in a lower QOS class than what is current on the calling thread. If the returned dispatch block object is submitted to a dispatch queue, the QOS class it will be executed with depends on the QOS class assigned to the block, the QOS class assigned to the queue and which of the following flags was specified or defaulted to: - DISPATCH_BLOCK_INHERIT_QOS_CLASS: default for asynchronous execution - DISPATCH_BLOCK_ENFORCE_QOS_CLASS: default for synchronous execution See description of dispatch_block_flags_t for details. If the returned dispatch block object is submitted directly to a serial queue and is configured to execute with a specific QOS class, the system will make a best effort to apply the necessary QOS overrides to ensure that blocks submitted earlier to the serial queue are executed at that same QOS class or higher. dispatch_block_notifySchedule a notification block to be submitted to a queue when the execution of a specified dispatch block object has completed. ( macos( 10.10), ios( 8.0)) void dispatch_block_notify( dispatch_block_t block, dispatch_queue_t queue, dispatch_block_t notification_block); Parameters
DiscussionThis function will submit the notification block immediately if execution of the observed block object has already completed. It is not possible to be notified of multiple executions of the same block object with this interface, use dispatch_group_notify() for that purpose. A single dispatch block object may either be observed one or more times and executed once, or it may be executed any number of times. The behavior of any other combination is undefined. Submission to a dispatch queue counts as an execution, even if cancellation (dispatch_block_cancel) means the block's code never runs. If multiple notification blocks are scheduled for a single block object, there is no defined order in which the notification blocks will be submitted to their associated queues. dispatch_block_performCreate, synchronously execute and release a dispatch block object from the specified block and flags. ( macos( 10.10), ios( 8.0)) DISPATCH_NONNULL2 void dispatch_block_perform( dispatch_block_flags_t flags, DISPATCH_NOESCAPE dispatch_block_t block); ParametersDiscussionBehaves identically to the sequence
dispatch_block_testcancelTests whether the given dispatch block object has been canceled. ( macos( 10.10), ios( 8.0)) intptr_t dispatch_block_testcancel( dispatch_block_t block); ParametersReturn ValueNon-zero if canceled and zero if not canceled. dispatch_block_waitWait synchronously until execution of the specified dispatch block object has completed or until the specified timeout has elapsed. ( macos( 10.10), ios( 8.0)) DISPATCH_NONNULL1 intptr_t dispatch_block_wait( dispatch_block_t block, dispatch_time_t timeout); ParametersReturn ValueReturns zero on success (the dispatch block object completed within the specified timeout) or non-zero on error (i.e. timed out). DiscussionThis function will return immediately if execution of the block object has already completed. It is not possible to wait for multiple executions of the same block object with this interface; use dispatch_group_wait() for that purpose. A single dispatch block object may either be waited on once and executed once, or it may be executed any number of times. The behavior of any other combination is undefined. Submission to a dispatch queue counts as an execution, even if cancellation (dispatch_block_cancel) means the block's code never runs. The result of calling this function from multiple threads simultaneously with the same dispatch block object is undefined, but note that doing so would violate the rules described in the previous paragraph. If this function returns indicating that the specified timeout has elapsed, then that invocation does not count as the one allowed wait. If at the time this function is called, the specified dispatch block object has been submitted directly to a serial queue, the system will make a best effort to apply the necessary QOS overrides to ensure that the block and any blocks submitted earlier to that serial queue are executed at the QOS class (or higher) of the thread calling dispatch_block_wait(). Typedefsdispatch_block_flags_ttypedef enum dispatch_block_flags dispatch_block_flags_t; Constants
DiscussionFlags to pass to the dispatch_block_create* functions. |