Async in Swift 201

İbrahim Oktay
Mac O’Clock
Published in
3 min readMay 6, 2020

This post is about DispatchQueues and Asynchronous Programming. What is a DispatchQueue? How to use? and What Problems you might encounter when using?

Part 1 | Part 2 | Part 3 | Part 4

What is a DispatchQueue? Here is Apple’s answer

An object that manages the execution of tasks serially or concurrently on your app’s main thread or on a background thread.

As the name suggests, it is a FIFO (First-in-First-out) queue. Just not that simple, it is very powerful and save developers from spending so much time for threading. Making synchronous and asynchronous calls easy for you. There are some queues already exists and you don’t need to create them in order to use them. DispatchQueue.main is one of them and it is the most important one because this queue is responsible for butter smooth UI experience. In other words, its job is to draw 60 fps (frame per second). In Mobile development, both iOS or Android, main thread is the UI thread. If you are developing a backend application, it changes because you do not have a UI-thread. It is just a thread from thread pool. But it is still important because it is the main-thread and you still should not block it.

There are background threads for heavy tasks. DispatchQueue.global() runs these kind of tasks in background threads. You can tell the queue about how important your task is, so that DispatchQueue can prioritize your task. You can do this by providing Quality-of-Service information. I am not going into this, you can find many post about it.

Queues can be serial or concurrent. These topics are also important. So, I advice you to learn about them. They both support sync/async code.

How to use?

DispatchQueue.global().sync { ... }
DispatchQueue.global().async { ... }
DispatchQueue.main.async { ... }

Don’t dos

DispatchQueue.main.sync { ... }

It will crash, so don’t do it.

Attempting to synchronously execute a work item on the main queue results in deadlock.

Do not update UI from a background thread. (Bad idea)

Also hear what apple says about: Avoiding Excessive Thread Creation. Here is the link.

Common Asynchronous Programming Problems in iOS development

  1. If your using the code below to fix your UI, you are probably doing something wrong.
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { ... }

2. If you have choppy/laggy UI (animations, scrolling ..etc), you might be doing excessive work in main-thread which cause dropping frames. In this case, you need to profile your app for long running-tasks. Then, you might need to do some of these long running tasks in a background-thread.

3. If you have something like below, It is not wrong but it is a bad practice. It is called Callback Hell. Do not do it.

service.getSomething() { result in
service.getSomethingElse() { result1 in
service.getSomethingUnique() { result2 in
...
}
}
}

There are also common multi-threading problems like Race Conditions, Deadlock and Starvation.

In the next article, I will write about how to create Race-Conditions and how to solve it with DispatchQueue and other alternatives. Any feedback would be appreciated. Please, reach me out for your questions.

Part 1 | Part 2 | Part 3 | Part 4

--

--