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)
By default, DispatchQueue is a serial queue. E.g. As you can see in the Output, the code is executed in the order it was defined, even though we used the async( ) function, the first thread took 3 seconds for completion and after that, the second thread was executed which took another second to complete.
In general, dispatch queues will only perform one task at a time in a first-in, first-out kind of fashion. They can, however, be configured to schedule work concurrently. The main dispatch queue is a queue that runs one task at a time.
A dispatch queue that is bound to the app's main thread and executes tasks serially on that thread. A dispatch queue that executes tasks concurrently using threads from the global thread pool. A dispatch queue that executes tasks serially in first-in, first-out (FIFO) order.
A dispatch queue executes tasks serially or concurrently on its associated thread. If you want to read more about dispatch queues, I encourage you to read my article Concurrent vs Serial DispatchQueue: Concurrency in Swift explained.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With