DispatchQueue 中的目标参数 [英] target parameter in DispatchQueue

查看:78
本文介绍了DispatchQueue 中的目标参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 3 中,创建一个 DispatchQueue 实例:

In Swift 3, the creation of a DispatchQueue instance:

DispatchQueue(label: String,
              qos: DispatchQoS,
              attributes: DispatchQueue.Attributes,
              autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency, 
              target: DispatchQueue?)

我看到 StackOverFlow 的示例代码,可以是 nil、.global() 或 .main,这个 target 参数是什么意思?

I see the sample codes from StackOverFlow, it can be nil, .global() or .main, what's the meaning of this target parameter?

我猜 .main 意味着队列将在主线程上运行,但是 .nil 或 .global() 呢?

I guess .main means the queue will run on main thread, but what for .nil or .global() ?

推荐答案

Swift 没有文档,所以我又回到了 GCD 的旧文档.我发现最接近的是函数 dispatch_set_target_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_get_global_queue 函数获取所需优先级的合适目标队列.

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.

看起来 target 队列做了两件事:

So looks like the target queue does 2 things:

  1. 为您的新队列提供优先权
  2. 执行队列中所有对象的终结器(deinit)

从字里行间看,你的队列和目标队列之间有一些同步.我目前没有 Xcode,所以无法测试.

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.

这篇关于DispatchQueue 中的目标参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆