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)

technerd
  • 14,144
  • 10
  • 61
  • 92

2 Answers2

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • this fails in swift 4. What to specify in attributes, autoreleaseFrequency and target? the documentation is so poor – nr5 Oct 10 '18 at 11:30
  • @nr5: How does it fail? The above code still compiles in my Xcode 10 with Swift 4.2. – Martin R Oct 10 '18 at 11:36
  • 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: https://developer.apple.com/documentation/dispatch/dispatchqueue/2300059-init Please Help ! May be you can answer this question asked by someone: https://stackoverflow.com/questions/50129528/how-to-create-a-serial-queue-in-swift4 – nr5 Oct 10 '18 at 11:59
  • 1
    @nr5: All parameters have default values, so `DispatchQueue(label: "label", qos: .background)` should work. – Martin R Oct 10 '18 at 12:02
  • 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 keyword – nr5 Oct 10 '18 at 12:13
  • 1
    @nr5: It's all in the Swift Language Reference :) Look for “Default Parameter Values” in https://docs.swift.org/swift-book/LanguageGuide/Functions.html. – Martin R Oct 10 '18 at 12:24
  • 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" – nr5 Oct 10 '18 at 12:29
  • @nr5: Swift way, not iOS way, but essentially yes: the default parameter values is not exposed to the public interface, compare https://stackoverflow.com/questions/24991791/default-keyword-in-swift-parameter. That seems to change in Swift 5, see Hamish's comment here: https://stackoverflow.com/questions/52392998/difficulties-to-assign-default-value-to-a-parameter-of-a-function/52393108#comment91731284_52392998. – Martin R Oct 10 '18 at 12:35
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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98