How to create dispatch queue in Swift 3

前端 未结 15 1881
余生分开走
余生分开走 2020-11-22 16:35

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

let concurrentQueue = dispatch_queue_create(\"com.swift3.imageQueue\", DISPATCH_QUEUE_CONCURR         


        
相关标签:
15条回答
  • 2020-11-22 17:35

    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*/
     }
    
    0 讨论(0)
  • 2020-11-22 17:36
    DispatchQueue.main.async(execute: {
    
    // write code
    
    })
    

    Serial Queue :

    let serial = DispatchQueue(label: "Queuename")
    
    serial.sync { 
    
     //Code Here
    
    }
    

    Concurrent queue :

     let concurrent = DispatchQueue(label: "Queuename", attributes: .concurrent)
    
    concurrent.sync {
    
     //Code Here
    }
    
    0 讨论(0)
  • 2020-11-22 17:37

    Since the OP question has already been answered above I just want to add some speed considerations:

    It makes a lot of difference what priority class you assign to your async function in DispatchQueue.global.

    I don't recommend running tasks with the .background thread priority especially on the iPhone X where the task seems to be allocated on the low power cores.

    Here is some real data from a computationally intensive function that reads from an XML file (with buffering) and performs data interpolation:

    Device name / .background / .utility / .default / .userInitiated / .userInteractive

    1. iPhone X: 18.7s / 6.3s / 1.8s / 1.8s / 1.8s
    2. iPhone 7: 4.6s / 3.1s / 3.0s / 2.8s / 2.6s
    3. iPhone 5s: 7.3s / 6.1s / 4.0s / 4.0s / 3.8s

    Note that the data set is not the same for all devices. It's the biggest on the iPhone X and the smallest on the iPhone 5s.

    0 讨论(0)
提交回复
热议问题