---
title: "How to use dispatch_async, dispatch_sync in Objective-C?"  
description: "How to use dispatch_async, dispatch_sync in Objective-C?"  
author: "Anonymous User"  
published: 2016-01-13  
updated: 2016-02-20  
canonical: https://www.mindstick.com/forum/33866/how-to-use-dispatch_async-dispatch_sync-in-objective-c  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# How to use dispatch_async, dispatch_sync in Objective-C?

I have a need to create serial [queue](https://www.mindstick.com/forum/160388/describe-the-basic-features-and-use-cases-of-the-queue-and-stack-collections-in-c-sharp) in our [iPhone app](https://www.mindstick.com/articles/208914/4life-innovations-is-bracing-up-to-acquire-ios-13-for-iphone-app-development). I use **dispatch_queue_t** in our [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) like this:

```
dispatch_queue_t MyQueue = dispatch_queue_create("My Queue", DISPATCH_QUEUE_SERIAL);
```

But I want to know the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between **dispatch_sync** and **dispatch_async**.\
Can [anyone explain](https://www.mindstick.com/forum/34220/can-anyone-explain-me-about-off-page-activity) me both [differences](https://www.mindstick.com/articles/12918/cat-5e-vs-cat-6a-understanding-the-major-differences)? and which will be the best to use in our code.\
Thank you.

## Replies

### Reply by Tarun Kumar

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
    });
});
```

I hope the the above explanation is sufficient.


---

Original Source: https://www.mindstick.com/forum/33866/how-to-use-dispatch_async-dispatch_sync-in-objective-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
