3

I have code like this in Swift 2:

let attrs = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0)
let myQueue = dispatch_queue_create("com.example.serial-queue", attrs)

This doesn't compile in Swift 3, because dispatch_queue_attr_make_with_qos_class and dispatch_queue_create aren't available. How do I make a serial queue with a custom QoS class?

jtbandes
  • 115,675
  • 35
  • 233
  • 266

1 Answers1

7

DispatchQueue is now a class, and you can use its init(label:attributes:target:) initializer. The attributes are now an OptionSet called DispatchQueueAttributes, which has instances .serial and .qosUtility.

Putting it together:

let myQueue = DispatchQueue(label: "com.example.serial-queue",
                            attributes: [.serial, .qosUtility])
Graham
  • 7,431
  • 18
  • 59
  • 84
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 3
    In the last version that I tested of Swift 3 (which is XCode 8 Beta 6), this declaration gives the error message "Type 'DispatchQueue.Attributes' has no member 'serial'". Any idea on how to force it to be a serial queue? – nbloqs Aug 30 '16 at 18:21
  • 4
    Serial is the default. – jtbandes Aug 30 '16 at 18:22