How to create dispatch queue in Swift 3

2018-12-31 18:35发布

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 doesn't compile in Swift 3.

What is the preferred way to write this in Swift 3?

14条回答
闭嘴吧你
2楼-- · 2018-12-31 18:41

Compiles under Swift 3. This example contains most of the syntax that we need.

QoS - new quality of service syntax

weak self - to disrupt retain cycles

if self is not available, do nothing

async global background queue - for network query

async main queue - for touching the UI.

Of course, you need to add some error checking to this...

DispatchQueue.global(qos: .background).async { [weak self] () -> Void in

    guard let strongSelf = self else { return }

    strongSelf.flickrPhoto.loadLargeImage { loadedFlickrPhoto, error in

        if error != nil {
            print("error:\(error)")
        } else {
            DispatchQueue.main.async { () -> Void in
                activityIndicator.removeFromSuperview()
                strongSelf.imageView.image = strongSelf.flickrPhoto.largeImage
            }
        }
    }
}
查看更多
呛了眼睛熬了心
3楼-- · 2018-12-31 18:42

Creating a concurrent queue

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

}  

Create a serial queue

let serialQueue = DispatchQueue(label: "queuename")
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(qos: .background).async {

}

Xcode 8.2 beta 2:

To get one of the background thread

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

}

DispatchQueue.global().async {
    // qos' default value is ´DispatchQoS.QoSClass.default`
}

If you want to learn about using these queues .See this answer

查看更多
看淡一切
4楼-- · 2018-12-31 18:44
   let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT) //Swift 2 version

   let concurrentQueue = DispatchQueue(label:"com.swift3.imageQueue", attributes: .concurrent) //Swift 3 version

I re-worked your code in Xcode 8, Swift 3 and the changes are marked in contrast to your Swift 2 version.

查看更多
骚的不知所云
5楼-- · 2018-12-31 18:46

For Swift 3

   DispatchQueue.main.async {
        // Write your code here
    }
查看更多
牵手、夕阳
6楼-- · 2018-12-31 18:48

I did this and this is especially important if you want to refresh your UI to show new data without user noticing like in UITableView or UIPickerView.

    DispatchQueue.main.async
 {
   /*Write your thread code here*/
 }
查看更多
还给你的自由
7楼-- · 2018-12-31 18:51

Swift 3

you want call some closure in swift code then you want to change in storyboard ya any type off change belong to view your application will crash

but you want to use dispatch method your application will not crash

async method

DispatchQueue.main.async 
{
 //Write code here                                   

}

sync method

DispatchQueue.main.sync 
{
     //Write code here                                  

}
查看更多
登录 后发表回答