[英]Can asynchronous operations be used with `progress` on OperationQueue?
[英]Restrict count operations in OperationQueue
我需要限制并发操作队列中的操作数。 为此,我使用operationsCount
属性和waitUntilAllOperationsAreFinished
。
func exec(_ block: @escaping ()-> Void) {
self.queue.addOperation(block)
if (self.queue.operationCount == self.queue.maxConcurrentOperationCount)
{
self.wait()
}
}
func wait() {
self.queue.waitUntilAllOperationsAreFinished()
}
在iOS 13中, 不建议使用 operationsCount
。
我知道我可以将DispatchSemaphore
与最大并行操作数作为值使用:
let semaphore = DispatchSemaphore(value: 10)
<...>
semaphore.wait(timeout: DISPATCH_TIME_FOREVER)
async{
work()
semaphore.signal()
}
但是在我的测试中, wait
和signal
没有足够的性能。 如何在iOS 13中获得操作计数?
您可以使用maxConcurrentOperationCount
信号量的所有麻烦。 限制maxConcurrentOperationCount
可以向队列中添加maxConcurrentOperationCount
数量的操作,但是只有指定的数量会同时执行。
看一下文档: https : //developer.apple.com/documentation/foundation/operationqueue/1414982-maxconcurrentoperationcount 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.