target parameter in DispatchQueue

时光怂恿深爱的人放手 提交于 2019-11-30 02:53:02

问题


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


回答1:


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.




回答2:


The target is explained by apple as:

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

https://developer.apple.com/reference/dispatch/1452989-dispatch_set_target_queue

1. .main will run on the main thread. The main thread is used primarily for UI work so you should be cautious when using this thread for work that is not UI related because it could make the UI hang or appear unresponsive. This queue has the highest priority.

2. .global is primarily used for other work that is not UI related. and schedules blocks when threads become available. the global queue has three priorities Low, Default & High. This queue has the second highest priority with 3 different types.

3. nil is the lowest priority and will be lower than any global queue. it has no priority, it just needs to get done.

Summary

.main as target for UI work

.global as target for other work that needs to be done as soon as possible

nil as target for work that just needs to get done at some point (your not bothered when)



来源:https://stackoverflow.com/questions/40634586/target-parameter-in-dispatchqueue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!