Re: How to use dispatch_async, dispatch_sync in Objective-C?

Posted on one year ago

If you want to execute any specific task before any specific operation then use it between the dispatch_sync block. The code written in the dispatch_sync block will execute first because dispatch_sync() function creates a simple deadlock, as the currently executing block will waits for the next block to complete but the next block will not start until the currently running block completes.

and the dispatch_async block will execute with default queue of your application main queue for example if we are downloading a file form the network and want to display updating in progress bar of downloads.

Example of dispatch_async() function:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
// this is the background thread
dispatch_async(dispatch_get_main_queue(), ^(void){
// here you can display the updates of the progress bar
});
});