Sobes.tech
Middle

What is Quality of Service?

sobes.tech AI

Answer from AI

Quality of Service (QoS) in iOS is a system that allows managing the priority of task execution (DispatchQueue). The OS uses this information to allocate resources (CPU time, power).

There are several levels of QoS:

  • UserInteractive: Tasks directly related to the UI that require immediate execution to maintain responsiveness (e.g., animation, touch event processing).
  • UserInitiated: Tasks initiated by the user but not requiring instant response (e.g., loading content for display after a short wait).
  • Utility: Long-running tasks that do not block the UI but require some time (e.g., background data loading, computations).
  • Background: Tasks running in the background and invisible to the user (e.g., data synchronization).
  • Default: Default priority, used if QoS is not explicitly specified. It is between Utility and Background.
  • Unspecified: Indicates that QoS should not be used or is passed from the calling context.
// Example of creating a queue with UserInitiated QoS
let userInitiatedQueue = DispatchQueue.global(qos: .userInitiated)

userInitiatedQueue.async {
    // Execute a high-priority task initiated by the user
}

// Example of creating a queue with Background QoS
let backgroundQueue = DispatchQueue.global(qos: .background)

backgroundQueue.async {
    // Execute a background task
}

Using the correct QoS helps optimize energy consumption, improve UI responsiveness, and utilize system resources more efficiently. The task priority can be lowered but not raised relative to the queue's priority. If a task is passed to another queue with a lower priority, it will retain its original higher priority.