微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – DispatchQueue中的目标参数

Swift 3中,创建了一个dispatchQueue实例:
dispatchQueue(label: String,qos: dispatchQoS,attributes: dispatchQueue.Attributes,autoreleaseFrequency: dispatchQueue.AutoreleaseFrequency,target: dispatchQueue?)

我看到来自StackOverFlow的示例代码,它可以是nil,.global()或.main,这个目标参数的含义是什么?

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

Swift没有文档,所以我回到了GCD的旧文档.我发现的最接近函数 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.

所以看起来目标队列做了两件事:

>为新队列提供priorty
>执行队列中所有对象的终结器(deinit)

在行之间读取,队列和目标队列之间存在一些同步.我目前没有Xcode,所以我无法测试.

原文地址:https://www.jb51.cc/swift/318675.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐