让我们假设一个异步执行的方法,需要一些时间才能完成:
public func performOperation(_ number: Int,success: @escaPing (Int) -> VoID)->VoID { dispatchQueue(label: "operations").async { print("Operation #\(number) starts") usleep(useconds_t(1000-number*200)) // Block thread for some time success(number) }}
*** 作和依赖项创建如下:
let operationQueue = OperationQueue.mainfor operationNumber in 0..<4 { // Create operations as an example let operation = BlockOperation(block: { performOperation(operationNumber) { number in dispatchQueue.main.sync { print("Operation #\(number) finished") } } }) operation.name = "Operation #\(operationNumber)" if operationNumber > 0 { operation.addDependency(operationQueue.operations.last!) // Print dependencIEs print("\(operation.name!) should finish after \(operation.dependencIEs.first!.name!)") } operationQueue.addOperation(operation)}
使用以下输出:
Operation #1 should finish after Operation #0Operation #2 should finish after Operation #1Operation #3 should finish after Operation #2Operation #0 startsOperation #1 startsOperation #2 startsOperation #3 startsOperation #0 finishedOperation #3 finishedOperation #2 finishedOperation #1 finished
这显然是不正确的.似乎OperationQueue只确保 *** 作以正确的顺序启动(而不是一个接一个地完成).
尽管可以使用dispatchSemaphore执行此 *** 作,但我想知道是否也可以使用OperationQueue.
我认为你在这里可能会感到困惑的是认为反复说明dispatchQueue(标签:“ *** 作”)会使你获得相同的串行调度队列实例 – 事实并非如此,实际上你在每个调用时创建一个新的串行队列.
另外,也没有理由在performOperation中创建或调度到串行调度队列,因为BlockOperation已经实现,以便在支持OperationQueue(the concurrency is also possible to limit)的GCD调度队列上同时执行块.在我的例子中,我要做的是使用OperationQueue()构造一个新的OperationQueue(而不是使用在主队列上调度工作的OperationQueue.main),然后异步地将成功回调调度到主队列.
这个稍微修改过的例子向您展示了 *** 作执行确实遵循了依赖关系(我没有实现上面的OperationQueue相关建议,它可以说是你提出的问题的旁边):
public func performOperation(_ number: Int,success: @escaPing (Int) -> VoID)->VoID { print("Operation #\(number) starts") usleep(useconds_t(1000-(number*50))) // Block thread for some time success(number)}… let operationQueue = OperationQueue.mainfor operationNumber in 0..<8 { // Create operations as an example let operation = BlockOperation(block: { self.performOperation(operationNumber) { number in print("Operation #\(number) finished") } }) operation.name = "Operation #\(operationNumber)" if operationNumber > 0 { operation.addDependency(operationQueue.operations.last!) // Print dependencIEs print("\(operation.name!) should finish after \(operation.dependencIEs.first!.name!)") } operationQueue.addOperation(operation)}
这将输出……
Operation #1 should finish after Operation #0Operation #2 should finish after Operation #1Operation #3 should finish after Operation #2Operation #4 should finish after Operation #3Operation #5 should finish after Operation #4Operation #6 should finish after Operation #5Operation #7 should finish after Operation #6Operation #0 startsOperation #0 finishedOperation #1 startsOperation #1 finishedOperation #2 startsOperation #2 finishedOperation #3 startsOperation #3 finishedOperation #4 startsOperation #4 finishedOperation #5 startsOperation #5 finishedOperation #6 startsOperation #6 finishedOperation #7 startsOperation #7 finished总结
以上是内存溢出为你收集整理的ios – 如何确保OperationQueue中的 *** 作一个接一个地完成全部内容,希望文章能够帮你解决ios – 如何确保OperationQueue中的 *** 作一个接一个地完成所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)