swift BUTCHER swift BUTCHER - 5 months ago 1825
iOS Question

How to create dispatch queue in swift 3

In Swift 2, I was able to create queue with the following code:

let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)


But this doesnot compile in Swift 3. What is the preferred way to write this in Swift 3?

Answer

Creating a concurrent queue

let concurrentQueue = DispatchQueue(label: "queuename", attributes: .concurrent)
concurrentQueue.sync {

}  

Create a serial queue

let serialQueue = DispatchQueue(label: "queuename", attributes: .serial)
serialQueue.sync { 

}

Get main queue asynchronously

DispatchQueue.main.async {

}

Get main queue synchronously

DispatchQueue.main.sync {

}

To get one of the background thread

DispatchQueue.global(attributes: .qosDefault).async {

}