How to create dispatch queue in Swift 3

前端 未结 15 2165
余生分开走
余生分开走 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:29

    Update for swift 5

    Serial Queue

    let serialQueue = DispatchQueue.init(label: "serialQueue")
    serialQueue.async {
        // code to execute
    }
    

    Concurrent Queue

    let concurrentQueue = DispatchQueue.init(label: "concurrentQueue", qos: .background, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil)
    
    concurrentQueue.async {
    // code to execute
    }
    

    From Apple documentation:

    Parameters

    label

    A string label to attach to the queue to uniquely identify it in debugging tools such as Instruments, sample, stackshots, and crash reports. Because applications, libraries, and frameworks can all create their own dispatch queues, a reverse-DNS naming style (com.example.myqueue) is recommended. This parameter is optional and can be NULL.

    qos

    The quality-of-service level to associate with the queue. This value determines the priority at which the system schedules tasks for execution. For a list of possible values, see DispatchQoS.QoSClass.

    attributes

    The attributes to associate with the queue. Include the concurrent attribute to create a dispatch queue that executes tasks concurrently. If you omit that attribute, the dispatch queue executes tasks serially.

    autoreleaseFrequency

    The frequency with which to autorelease objects created by the blocks that the queue schedules. For a list of possible values, see DispatchQueue.AutoreleaseFrequency.

    target

    The target queue on which to execute blocks. Specify DISPATCH_TARGET_QUEUE_DEFAULT if you want the system to provide a queue that is appropriate for the current object.

提交回复
热议问题