To create a serial queue, normally you would write something simple like this:

let queue = DispatchQueue(label: “com.mydomain.myqueue")

If you wanted to define a priority for that queue, you could have done that by retrieving a global queue of the required priority and setting it as a target. But that’s the old way to do it and it doesn’t work in Swift 3. Since Swift 3, once a dispatch queue is activated, it cannot be mutated anymore. Setting a target on an activated queue will compile but then throw an error in run time.

Fortunately, DispatchQueue initializer accepts other arguments besides label and one of them is qos.

let queue = DispatchQueue(label: "com.mydomain.myqueue", qos: .background)

Although Apple documentation states this initializer is available on iOS 10+, I’ve tested it and it also works on iOS 9.

If for whatever reason, you still need to set the target on an already created queue, you can do that by using the initiallyInactive attribute available since iOS 10:

let queue = DispatchQueue(label: “com.mydomain.myqueue”, attributes: [.initiallyInactive])

That will allow you to modify it until you activate it.

References


[Back to Top]