[GCD] DispatchQueue — Defined Quality of service (QoS)

A note from Concurrency by tutorials — Chapter 3 queues and threads to choose a proper defined QoS when creating a concurrent dispatch queue …

Chien Pham
2 min readAug 20, 2022
  • When using a concurrent dispatch queue, we have to specify how important the task is, then the OS will give it a proper priority.
  • Higher-priority work has to be performed faster, likely taking more system resources to complete and requiring more energy than lower-priority work.
  • If you just need a concurrent queue but don’t want to manage your own, you can use the global class method on DispatchQueue to get one of the pre-defined global queues:

let queue = DispatchQueue.global(qos: .userInteractive)

Apple offers six quality of service classes:

.userInteractive

The .userInteractive QoS is recommended for tasks that the user directly interacts with. UI-updating calculations, animations or anything needed to keep the UI responsive and fast. If the work doesn’t happen quickly, things may appear to freeze. Tasks submitted to this queue should complete virtually instantaneously.

.userInitiated

The .userInitiated queue should be used when the user kicks off a task from the UI that needs to happen immediately, but can be done asynchronously. For example, you may need to open a document or read from a local database. If the user clicked a button, this is probably the queue you want. Tasks performed in this queue should take a few seconds or less to complete.

.utility

You’ll want to use the .utility dispatch queue for tasks that would typically include a progress indicator such as long-running computations, I/O, networking or continuous data feeds. The system tries to balance responsiveness and performance with energy efficiency. Tasks can take a few seconds to a few minutes in this queue.

.background

For tasks that the user is not directly aware of you should use the .background queue. They don’t require user interaction and aren’t time sensitive. Prefetching, database maintenance, synchronizing remote servers and performing backups are all great examples. The OS will focus on energy efficiency instead of speed. You’ll want to use this queue for work that will take significant time, on the order of minutes or more.

.default

.default option falls between .userInitiated and .utility and is the default value of the qos argument. It’s not intended for you to directly use.

.unspecified

.unspecified exists to support legacy APIs that may opt the thread out of a quality of service. It’s good to know they exist, but if you’re using them, you’re almost certainly doing something wrong.

Good to know both .default and .unspecified options that exist, but you should not use them explicitly.

Note: Global queues are always concurrent and first-in, first-out.

--

--