What is QoS (Quality of Service) in GCD? - Swift

Note: If you are a beginner and you find it difficult to understand things written in this article. Feel free to ask questions on my Reddit page or Twitter.

To Read, How to use GCD (Grand Central Dispatch) in Swift? - Synchronous & Asynchronous Programming Tutorial. Click Here.

To Read, Difference between Concurrent Queue and Serial Queue. Click Here.

To Read, How to group dispatch queues in Swift? Click Here.

Remember, you can do it. 😉


Hello Fellas, I hope you guys have read the articles I have mentioned on the top of the page for better understanding.

It’s better when your system handles the core and task execution automatically, but sometimes we need things to be done at priority.

QoS (Quality of Service)

Defining quality of service helps us categorize the task of our DispatchQueue with priority. But this doesn’t mean that we should schedule all the tasks at a high priority. By using it the right way, we could make our app energy efficient and responsive.

Let’s see how we can use QoS in our code.

let queue = DispatchQueue(label: "com.swiftpal.dispatch.qos")

// async with background type
queue.async(qos: .background) {
    print("Background Code")
}

// async with userInitiated type
queue.async(qos: .userInitiated) {
    print("User Initiated Code")
}

QoS can be used with .async() function, and the priorities are divided into four main categories.

  • userInteractive: Used for animations, or updating UI.

  • userInitiated: Used for tasks like loading data from API, preventing the user from making interactions.

  • utility: Used for tasks that do not need to be tracked by the user.

  • background: Used for tasks like saving data in the local database or any maintenance code which is not on high priority.

Try to categorize your code while using DispatchQueue’s, so that the apps you are developing become energy efficient and responsive.

Once you categorize your task, the system handles it the best way possible constrained to the availability of resources. Another point to note is that utility and background have low priority over the others, and we should try to reason our code and use all of them according to our needs. These warnings are just because if you use it incautiously, then as I said in my previous articles, your app will face Thread Explosion and Deadlock.

I hope you liked my article, there is so much more to explore in GCD, so subscribe to my article now and receive the latest updates about my new article.

If you have any suggestions or questions, Feel free to connect me on Twitter. 😉

To Read, What is a DispatchWorkItem in GCD (Grand Central Dispatch)? - Swift, Click Here.

What is a DispatchWorkItem in GCD (Grand Central Dispatch)? - Swift

How to group dispatch queues in Swift? (GCD - Grand Central Dispatch) Handling Completion of DispatchQueue