我知道您可以通过执行以下操作来创建并发队列:

let queue = DispatchQueue(label: "My Awesome Queue", attributes: .concurrent)

但我没有看到用于创建序列队列的替代枚举,类似于:

let queue = DispatchQueue(label: "My Awesome Queue", attributes: .serial)

The only other possible option seems to be .initiallyInactive, am I missing something?

如何在SWIFT中指定我需要一个串行队列?

Note that I am using the above queues like this:

queue.async {
    // do task 1
}

queue.async {
    // do task 2
}

// expect task 1 to start
// expect task 1 to finish
// expect task 2 to start
// expect task 2 to finish

推荐答案

let queue = DispatchQueue(label: "My Awesome Queue", attributes: .concurrent)

当您使用具有.concurrent属性的DispatchQueue时.它将以并发方式执行.

let queue = DispatchQueue(label: "My Awesome Queue")

但如果不存在该属性,队列将以先进先出(FIFO)的顺序连续调度任务.有关更多详细信息,请查看文档here.

Btw:.initiallyInactive属性用于防止队列调度块,直到您调用它的activate()方法.

Swift相关问答推荐

通过withstickedContinuation传递通用类型T

SwiftUI—如何识别单词并获取视觉中的位置

如何强制创建新的UIViewControllerRenatable|更新Make UIViewController上的视图

OSX 中的 Popover 无法确定透明度

为什么 UITapGestureRecognizer 对于 Swift 集合视图中的单元格图像无法正常工作?

如何在 SwiftUI 中创建条件内容?

如何在 swift 5.0 中获取当前行

有没有更快的方法来循环浏览 macOS 上已安装的应用程序?

并发执行代码中捕获的 var 的变异

在主要参与者中启动分离任务和调用非隔离函数之间的区别

这是 Int64 Swift Decoding 多余的吗?

为什么 SwiftUI 不在工具栏菜单中反映 @State 属性值?

阻止其他类型的键盘

RxSwift:share() 替代方案,保证upstream 的单一订阅

如何在 iOS Swift 中进行多线程、并发或并行?

MKAnnotation Swift

Swift - 迭代 struct 对象时如何对其进行变异

使用相机进行人脸检测

如何在 Swift 中判断变量的类型

swift 中的@property/@synthesize 类似功能