多线程在swift ios中并行执行多个任务
我知道队列的创建,能够执行单个任务,但如何并行执行多个任务 并发队列--> 没有优先级的BackgroundQueue默认值---> 具有优先级的Backgroundqueue--> 返回主队列-->多线程在swift ios中并行执行多个任务,ios,swift,multithreading,swift4,dispatch-queue,Ios,Swift,Multithreading,Swift4,Dispatch Queue,我知道队列的创建,能够执行单个任务,但如何并行执行多个任务 并发队列--> 没有优先级的BackgroundQueue默认值---> 具有优先级的Backgroundqueue--> 返回主队列--> 所有这些都是异步的,但如何一次执行多个方法?如何使用swift编写代码 如果您有一个for循环方法调用一个方法,并且您希望并发调用该方法,那么只需使用以下方法: DispatchQueue.concurrentPerform(iterations: Int, execute: { (count)
所有这些都是异步的,但如何一次执行多个方法?如何使用swift编写代码 如果您有一个for循环方法调用一个方法,并且您希望并发调用该方法,那么只需使用以下方法:
DispatchQueue.concurrentPerform(iterations: Int, execute: { (count) in
doSomethingFor(count: count)
}
但如果您有一些要调用concurrent的方法,请执行以下操作:
let concurrentQueue = DispatchQueue(label: "com.some.concurrentQueue", attributes: .concurrent)
concurrentQueue.async {
//executable code
myFirstMethod()
}
concurrentQueue.async {
//executable code
mySecondMethod()
}
通过这种方式,concurrentQueue将同时管理您的任务。通过
DispatchQUE
很难处理这个问题。相反,您可以使用操作
和操作QEUU
。在这种情况下,您可以设置您想要的最大并发任务数。这超出了这里的解释范围,但是如果您想要一个关于这方面的好教程,请看本教程:请不要试图通过编辑问题文本来删除您的问题。如果您有正当理由希望删除该问题,您应该向版主或网站管理员寻求帮助,请参阅以获取更多信息。
DispatchQueue.global(qos: .userInitiated).async { //.userInteractive .background .default .unspecified
//executable code
}
DispatchQueue.main.async {
//executable code
}
DispatchQueue.concurrentPerform(iterations: Int, execute: { (count) in
doSomethingFor(count: count)
}
let concurrentQueue = DispatchQueue(label: "com.some.concurrentQueue", attributes: .concurrent)
concurrentQueue.async {
//executable code
myFirstMethod()
}
concurrentQueue.async {
//executable code
mySecondMethod()
}