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.
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
}
Set the maximum number of concurrent operations to 1
then each operation will be executed in order (as if each was dependent on the previous one) and your completion operation will execute at the end.
You can use operation dependencies to initiate some operation upon the completion of a series of other operations:
Code at the end of the queue refer to this link
NSOperation and NSOperationQueue are great and useful Foundation framework tools for asynchronous tasks. One thing puzzled me though: How can I run code after all my queue operations finish? The simple answer is: use dependencies between operations in the queue (unique feature of NSOperation). It's just 5 lines of code solution.
NSOperation dependency trick with Swift it is just easy to implement as this:
A suitable solution is KVO
First before the loop add the observer (assuming
queue
is theOperationQueue
instance)Then implement
Edit:
In Swift 4 it's much easier
Declare a property:
and create the observer
I use the next solution: