How can I Pause a NSOperation in a NSOperationQueue?

Viewed 17368

I need to pause a running NSOperation which was inserted in an NSOperationQueue. Currently I am canceling all operations and restarting them. But this would lead to some kind of duplication in terms of process done. I tried with setSuspended flag of NSOperationQueue. But it's not suspending the operation. Is there any way out?

3 Answers

In Swift 5, you can use isSuspended property to pause and resume your OperationQueue, you can see the example for more understanding:-

let operationQueue = OperationQueue()

let op1 = BlockOperation {
    print("done")
}
let op2 = BlockOperation {
    print("op2")
}
let op3 = BlockOperation {
    print("op3")
}

op1.addDependency(op2)
operationQueue.addOperations([op1, op2, op3], waitUntilFinished: false)

operationQueue.isSuspended = true
print("operationQueue suspended")

if operationQueue.isSuspended {
    operationQueue.isSuspended = false
    print("operationQueue restarted")
}


OutPut:- 
op2
op3
operationQueue suspended
operationQueue restarted
done
Related