菜鸟教程小白 发表于 2022-12-11 18:13:14

ios - 如何在 Swift 3 中创建调度队列


                                            <p><p>在 Swift 2 中,我可以使用以下代码创建队列:</p>

<pre><code>let concurrentQueue = dispatch_queue_create(&#34;com.swift3.imageQueue&#34;, DISPATCH_QUEUE_CONCURRENT)
</code></pre>

<p>但这在 Swift 3 中无法编译。</p>

<p>在 Swift 3 中编写此代码的首选方式是什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>创建并发队列</p>

<pre><code>let concurrentQueue = DispatchQueue(label: &#34;queuename&#34;, attributes: .concurrent)
concurrentQueue.sync {

}
</code></pre>

<p>创建串行队列</p>

<pre><code>let serialQueue = DispatchQueue(label: &#34;queuename&#34;)
serialQueue.sync {

}
</code></pre>

<p>异步获取主队列</p>

<pre><code>DispatchQueue.main.async {

}
</code></pre>

<p>同步获取主队列</p>

<pre><code>DispatchQueue.main.sync {

}
</code></pre>

<p>获取后台线程之一</p>

<pre><code>DispatchQueue.global(qos: .background).async {

}
</code></pre>

<p><strong>Xcode 8.2 beta 2:</strong> </p>

<p>获取后台线程之一</p>

<pre><code>DispatchQueue.global(qos: .default).async {

}

DispatchQueue.global().async {
    // qos&#39; default value is ´DispatchQoS.QoSClass.default`
}
</code></pre>

<p>如果您想了解如何使用这些队列。请参阅 <a href="https://stackoverflow.com/questions/19179358/concurrent-vs-serial-queues-in-gcd/35810608#35810608" rel="noreferrer noopener nofollow">answer</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Swift 3 中创建调度队列,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44983965/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44983965/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Swift 3 中创建调度队列