If you want to delay execution of block for a set number of seconds, you can use dispatch_after()
in the following way to perform delayed actions in any queue you like. In the example below 3 is the number of seconds we want to delay by.
dispatch_after( dispatch_time( DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC ),
dispatch_get_current_queue(),^{ /* Your code goes here. */ } );
The dispatch_time()
function takes dispatch_time_t
as it's first argument (typedef of uint64_t
) which represents from when the delay is, and the number of nanoseconds past then until the block should be executed as it's second argument.
You can use this a more flexible version of -[NSObject performSelector:withObject:afterDelay:]
by passing dispatch_get_current_queue()
, or if you want to use it across threads, by passing a different queue such as the main (dispatch_get_main_queue()
) or a global queue( dispatch_get_global_queue()
).