繁体   English   中英

限制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()
}

但是在我的测试中, waitsignal没有足够的性能。 如何在iOS 13中获得操作计数?

您可以使用maxConcurrentOperationCount信号量的所有麻烦。 限制maxConcurrentOperationCount可以向队列中添加maxConcurrentOperationCount数量的操作,但是只有指定的数量会同时执行。

看一下文档: https : //developer.apple.com/documentation/foundation/operationqueue/1414982-maxconcurrentoperationcount

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM