Sync and async tasks in Swift Grand Central Dispatch

When working with Grand Central Dispatch you are able to dispatch works for a dispatch queue by two ways. Synchronously and asynchronously. In this article we’re going to discuss further each of them.

Asynchronous and synchronous are two different types of work execution, very common in concurrent systems. We need to understand these concepts in order to understand the behaviour of the dispatch queues and threads itself.

Let’s use a little of our imagination to make things easier here. Let’s say you’re a dad of two boys. One of them is pretty young and the second one is older. Both are going to school but in different times. In the morning the older one goes to the school. You pick him to the bus. Look to the bus leaving and then go home to do your tasks. Today is your day to clean up the house. At some point your older kid will come back of the school and walk home just by himself. You’re ok with it.

Now is evening and you have to pack the younger kid to the school. You help him to take a shower, dress clothes and have some food. You pick him to the bus. You see the bus leaving. And… My god, you’re unable to go back home. You’re so protective that you will still in the bus stop till your son get back. And time passes. Some people say you’re wasting your time. You don’t believe them. You know you have to wait for him. At some point the bus is back and your kid jump from the bus right on your arms. And you both walk together to home.

The older son is like an async task, you don’t wait for him to get back from the school to do your chores. The younger one is like a sync task. You have to wait for him to come back to resume your work.

Asynchrony refers to works executed in an independent flow of the callee flow. If we translate it to the dispatch queues language, it means that the work will be executed in a different thread of the callee. You’ll send your kid to school and get back to your tasks.

Synchrony, analogously to the last definition, is when the work is executed in the same flow of the callee. Which for dispatch queues means that the work will be executed in the same thread of the callee, most part of the time — yeah, there are some edge cases in here. So for a more superficial understanding of the situation, just imagine as your current flow will be blocked until the work you’re dispatching synchronously for the queue is completed. Or, you’ll send your kid to school, and wait for him to come back and just then resume your tasks.

How to dispatch works asynchronously with Swift?

With a reference for a dispatch queue you just have to call the async method and pass to it a closure as shown below. In this case, you’re going to ask the older boy to go to the school and right after it will continue your execution flow, which mean, execute the code right after the call of the async method.

How to dispatch works synchronously with Swift?

Here you do basically the same of the previous example, you just have to change async to sync. It will ask the second kid go to school, and will resume the execution, allowing the flow to continue, just after the method goToSchool is done.

When use each of them?

You may be asking yourself when each of them is useful. Well, here each situation is different, and this answer may change depending of things like the architecture you’re developing the system. But the core logic didn’t change. You use sync when you have/want to wait for the result of the methods you’re calling. You use synch when you don’t have to wait or at least you’re using strategies to synchronize the results of the methods called asynchronously when they’re done.

Computer Engineer + iOS Engineer. I am interested in Swift, Kotlin, Firebase, Data Structures, ASO & On Solving Real World Problems.

Computer Engineer + iOS Engineer. I am interested in Swift, Kotlin, Firebase, Data Structures, ASO & On Solving Real World Problems.