Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

target parameter in DispatchQueue

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() ?

like image 254
pzs7602 Avatar asked Nov 16 '16 14:11

pzs7602


People also ask

Is Dispatchqueue global serial or concurrent?

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.

What is global queue in IOS?

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.

What is global dispatch queue?

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.

What is default queue in Swift?

application is the default priority global concurrent queue.


Video Answer


1 Answers

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:

  1. Provide the priority for your new queue
  2. Executes the finalizer (deinit) of all objects in your queue

Reading 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.

like image 90
Code Different Avatar answered Sep 19 '22 12:09

Code Different