In Swift 3, the creation of a DispatchQueue instance:
DispatchQueue(label: String,
qos: DispatchQoS,
attributes: DispatchQueue.Attributes,
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency,
target: DispatchQueue?)
I see the sample codes from StackOverFlow, it can be nil, .global() or .main, what's the meaning of this target
parameter?
I guess .main means the queue will run on main thread, but what for .nil or .global() ?
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.
Global queues This is a common choice to perform non-UI work in the background. Global queques are Concurrent queues that are shared by the whole system. There are four such queues with different priorities : high, default, low, and background. The background priority queue is I/O throttled.
A dispatch queue that executes tasks serially in first-in, first-out (FIFO) order. typealias dispatch_queue_concurrent_t. A dispatch queue that executes tasks concurrently and in any order, respecting any barriers that may be in place.
application is the default priority global concurrent queue.
There's no documentation for Swift so I dropped back to the old documentation for GCD. The closest that I've found is for the function dispatch_set_target_queue
:
An object's target queue is responsible for processing the object. The target queue determines the queue on which the object's finalizer is invoked. In addition, modifying the target queue of some objects changes their behavior:
Dispatch queues:
A dispatch queue's priority is inherited from its target queue. Use the dispatch_get_global_queue function to obtain a suitable target queue of the desired priority.
If you submit a block to a serial queue, and the serial queue’s target queue is a different serial queue, that block is not invoked concurrently with blocks submitted to the target queue or to any other queue with that same target queue.
So looks like the target
queue does 2 things:
deinit
) of all objects in your queueReading between the lines, there are some sychronization between your queue and the target queue. I don't have Xcode at the moment so I can't test.
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