Search code examples
queuegrand-central-dispatchswift5

How to create a background priority SERIAL queue WITH A NAME, in Swift?


Ok, I can greate a global serial background queue like this

DispatchQueue.global(qos: .background).async {

}

but I want my queue to have a name. So I tried this code recommended by Apple (ha!):

let queueAttributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_PRIORITY_BACKGROUND, QOS_CLASS_BACKGROUND, 0)
let videoQueue = dispatch_queue_create("videoQueue", queueAttributes);

As expected, this fails pointing to DISPATCH_QUEUE_PRIORITY_BACKGROUND with this error

Cannot convert value of type 'Int' to expected argument type '__OS_dispatch_queue_attr?'

obviously clicking on the error or searching the web gives no clue and autocomplete does not give any clues on what to put there instead.

any ideas?


Solution

  • You can use:

    DispatchQueue(label: "name.of.your.queue")
    

    or

    let processingQueue = DispatchQueue(label: "your.queue", qos: .background,
                                        attributes: [],
                                        autoreleaseFrequency: .inherit,
                                        target: nil)
    

    Noted that DispatchQueue.Attributes is a OptionSet, you can pass empty [], or combine several values on it.