I have just started implementing some Operation Subclass' which handle some async work like downloading for me. I am curious about best practice for managing the queue, specifically to make sure i don't add the same task twice.
Is there a method where i can add the operation to the queue with a name or is it a case of creating and managing a dictionary? Say, when items are added to the queue you append entry to dictionary and when they are finished you remove entry from dictionary? Performing conditional check beforehand?
This could achieved pretty easily as there is a notification block within the operation subclass. It just seems a bit hacky.
Thanks for advice.
---- EDIT ----
So tried using this for loop elsewhere (cellForItemAt) to show activity indicator if item is in queue but it seems to only be checking the first item of the queue and return that but no others even if there are multiple operations in the queue with unique names:
for operation in downloadQueue.operations {
if operation.name == self.multiPartArray[collectionView.tag][indexPath.item].name { innerCell.contentView.addSubview(self.activityIndicatorView) self.activityIndicatorView.centerXAnchor.constraint(equalTo: innerCell.contentView.centerXAnchor).isActive = true self.activityIndicatorView.centerYAnchor.constraint(equalTo: innerCell.contentView.centerYAnchor).isActive = true
self.activityIndicatorView.isHidden = false
self.activityIndicatorView.startAnimating()
innerCell.contentView.bringSubview(toFront: self.activityIndicatorView)
break
} else {
print("Operation not in queue")
}
}
Seems to be doing the same thing when adding operations to the queue. It checks the first operation. If != opName then it will add the operation even if opName exist is in the queue but not the first item.