在执行任务之前等待队列中的所有操作完成 [英] Wait for all Operations in queue to finish before performing task

查看:166
本文介绍了在执行任务之前等待队列中的所有操作完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Operation子类和操作队列,maxConcurrentOperationCount = 1.

I have an Operation subclass and Operation queue with maxConcurrentOperationCount = 1.

这按顺序执行我的操作,我添加它们很好,但现在我需要等到所有操作都完成后再运行另一个进程。

This performs my operations in a sequential order that i add them which is good but now i need to wait until all operations have finished before running another process.

我试图使用通知组但是因为这一操作是在for循环中运行的添加到队列中的通知组触发..如何在运行另一个进程之前等待所有操作离开队列?

i was trying to use notification group but as this is run in a for loop as soon as the operations have been added to the queue the notification group fires.. How do i wait for all operations to leave the queue before running another process?

for (index, _) in  self.packArray.enumerated() {

    myGroup.enter()
    let myArrayOperation = ArrayOperation(collection: self.outerCollectionView, id: self.packArray[index].id, count: index)
    myArrayOperation.name = self.packArray[index].id
    downloadQueue.addOperation(myArrayOperation)
    myGroup.leave()

}

myGroup.notify(queue: .main) {
 // do stuff here
}


推荐答案

您可以使用操作依赖关系在完成某个操作后启动某些操作一系列其他操作:

You can use operation dependencies to initiate some operation upon the completion of a series of other operations:

let operationQueue = OperationQueue()

let completionOperation = BlockOperation {
    // do something
}

for object in objects {
    let operation = ...
    completionOperation.addDependency(operation)
    operationQueue.addOperation(operation)
}

OperationQueue.main.addOperation(completionOperation)

这篇关于在执行任务之前等待队列中的所有操作完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆