0
votes

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.

1
u can use for in for that.vaibby

1 Answers

1
votes

You can use the operation name to do this.

let yourOperationQueue = NSOperationQueue()

Set operation name every time when you add an operation and check with that name every time before you add the operation. Keep those operation names unique.

func addDownloadOperation()
{
    self.checkAndAddOperationWithName("DownloadOperation")
}

func addUploadOperation()
{
    self.checkAndAddOperationWithName("UploadOperation")
}

func checkAndAddOperationWithName(opName:String)
{
    var operationExist = false
    for operation in yourOpeartionQueue.operations
    {
        if operation.name == opName
        {
            print("Operation alreday added")
            operationExist = true
            break
        }
    }
    if !operationExist
    {
       self.addOperationToTheQueWithName(opName)
    }
}

func addOperationToTheQueWithName(opName:String)
{
    let someOperation = NSBlockOperation(block:{
        //some heavy operations
    })
    someOperation.name = opName
    yourOpeartionQueue.addOperation(someOperation)
    print("Operation \(opName) added")
}