Type 'DispatchQueue.Attributes' has no member 'serial'
Asked Answered
P

2

13

I have converted existing Swift2.3 code to Swift3.0 using Xcode8 beta4. Xcode automatically convert syntax to Swift3.0, but it not able to create serial dispatch queue.

private let serialQueue = DispatchQueue(label: "identifier", qos: DispatchQueue.Attributes.serial)

Pellikka answered 6/9, 2016 at 9:5 Comment(0)
I
27

There is not .serial attribute anymore, but dispatch queues are by default serial, unless you specify the .concurrent attribute:

let serialQueue = DispatchQueue(label: "label")
let concurrentQueue = DispatchQueue(label: "label", attributes: .concurrent)

Source: How to create a serial DispatchQueue in swift 3 with Xcode 8 beta 4? in the Apple Developer Forum.

Isidroisinglass answered 6/9, 2016 at 9:22 Comment(8)
this fails in swift 4. What to specify in attributes, autoreleaseFrequency and target? the documentation is so poorCircumsolar
@nr5: How does it fail? The above code still compiles in my Xcode 10 with Swift 4.2.Isidroisinglass
I wanted to create a serial queue and wanted to specify the qos only. But in Swift 4 there is 3 more params, attributes, frequency and target. If I use your code I cant specify qos. I cant make out of the documentation as well: developer.apple.com/documentation/dispatch/dispatchqueue/… Please Help ! May be you can answer this question asked by someone: #50130028Circumsolar
@nr5: All parameters have default values, so DispatchQueue(label: "label", qos: .background) should work.Isidroisinglass
great.. I know about convenience initializer but never saw this = default. I am fairly new to swift, can you point me to an article to understand the = default keywordCircumsolar
@nr5: It's all in the Swift Language Reference :) Look for “Default Parameter Values” in docs.swift.org/swift-book/LanguageGuide/Functions.html.Isidroisinglass
exactly, I knew this but in that example, there is a value. What is the meaning of default? Is this iOS defined way of saying "there's a value, but we won't tell you guyz for now"Circumsolar
@nr5: Swift way, not iOS way, but essentially yes: the default parameter values is not exposed to the public interface, compare #24992291. That seems to change in Swift 5, see Hamish's comment here: #52393498.Isidroisinglass
P
0

Use the DispatchQueueSerial or DispatchQueueConcurrent constructor, so you will get the right default values. Neither constructor lets you ask for either serial or concurrent execution, because that is built into the class. DispatchQueue will give you some incorrect default values.

Papiamento answered 1/9, 2023 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.