Clicky

iOS Dev Nugget 183 Quality of Service Classes

.

Need to run a code review on your codebase? Hire me

Quality of Service (QoS) classes are introduced in iOS 8. It's a concept that helps with making iOS energy efficient. QoS classes encompass priorities such as scheduling, CPU and I/O throughput and timer latency.

The primary QoS classes, shown in order of decreasing work importance are:

  • User-interactive — Work is virtually instantaneous
  • User-initiated — Nearly instantaneous, taking a few secs or less
  • Utility — Takes a few seconds to minutes
  • Background — Takes significant time. Minutes or hours

There are also two special QoS classes that we can't directly assign:

  • Default — falls between user-initiated and utility
  • Unspecified — QoS information absent, QoS class to be inferred.

When Grand Central Dispatch (GCD) was introduced, it supports four queue priorities. With the introduction of QoS classes, these queue (and the main thread's) priorities are mapped to the QoS classes:

  • Main thread maps to User-interactive QoS class
  • DISPATCH_QUEUE_PRIORITY_HIGH maps to User-initiated QoS class
  • DISPATCH_QUEUE_PRIORITY_DEFAULT maps to Default QoS class
  • DISPATCH_QUEUE_PRIORITY_LOW maps to Utility QoS class
  • DISPATCH_QUEUE_PRIORITY_BACKGROUND maps to Background QoS class

While the queue priorities can continue to be used with dispatch_get_global_queue(), it's recommended that the identifiers representing each QoS class be used with dispatch_get_global_queue instead:

  • QOS_CLASS_USER_INTERACTIVE
  • QOS_CLASS_USER_INITIATED
  • QOS_CLASS_UTILITY
  • QOS_CLASS_BACKGROUND

E.g.

let utilityGlobalQueue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)

The function dispatch_block_create_with_qos_class() has been added so you can use to apply a QoS class at the block level:

let block = dispatch_block_create_with_qos_class(0, QOS_CLASS_UTILITY) {
    //...
}
dispatch_async(myQueue, block)

There is also a qualityOfService property added to NSOperationQueue and NSOperation mirroring the identifiers counterparts mentioned earlier for GCD's queue and block.

The WWDC 2014 video session 710 provides a good introduction into QoS.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues