如何从 swift 中的 OperationQueue 中取消特定操作

How to cancel specific Operation from OperationQueue in swift

我的 OperationQueue 中有 3 个操作,我无法从它们中取消特定操作。

我提到了这个例子,但我无法理解 NSOperationQueue cancel specific operations

这是我的代码

class myOperation1 : Operation {

    override func main() {

        print("op1 () working....")

        for i in 1...10 {
            print("")
        }
    }
}

class myOperation2 : Operation {

    override func main() {

        print("op2 () working....")

        for i in 1...10 {
            print("")
        }
    }
}

class myOperation3 : Operation {

    override func main() {

        print("op3 () working....")
        for i in 1...10 {
            print("")
        }
    }
}

let op1 = myOperation1()
let op2 = myOperation2()
let op3 = myOperation3()

op1.completionBlock = {
    print("op1 () completed")
}

op2.completionBlock = {
    print("op2 () completed")
}

op3.completionBlock = {
    print("op3 () completed")
}

let opsQue = OperationQueue()
opsQue.addOperations([op1, op2, op3], waitUntilFinished: false)

DispatchQueue.global().asyncAfter(deadline: .now()) {
    opsQue.cancelAllOperations()
}

简而言之,我想取消 operationQueue 中的第二个操作。

请指导我。

谢谢

opsQue.cancelAllOperations() 在您的代码中导致从队列中删除 未启动的 操作并为每个正在执行的操作调用 Operation.cancel(),但它只设置 isCancelledtrue。你需要明确地处理它

class myOperation2 : Operation {

    override func main() {

        print("op2 () working....")

        for i in 1...10 {
            if self.isCancelled { break } // cancelled, so interrupt
            print("")
        }
    }
}

您可以调用 op2.cancel() 来取消操作,但是您需要采取额外的步骤才能真正从 运行 停止操作,因为 cancel() 仅设置 isCanceled 属性 为 true.

请查看开发者文档。 https://developer.apple.com/documentation/foundation/operation/1408418-iscancelled

The default value of this property is false. Calling the cancel() method of this object sets the value of this property to true. Once canceled, an operation must move to the finished state.

Canceling an operation does not actively stop the receiver’s code from executing. An operation object is responsible for calling this method periodically and stopping itself if the method returns true.

You should always check the value of this property before doing any work towards accomplishing the operation’s task, which typically means checking it at the beginning of your custom main() method. It is possible for an operation to be cancelled before it begins executing or at any time while it is executing. Therefore, checking the value at the beginning of your main() method (and periodically throughout that method) lets you exit as quickly as possible when an operation is cancelled.

希望您提到documentation for Operation

观察操作有几个 KVO-Compliant 属性。

有一个属性isCancelled - read-only

用于在执行操作前检查此属性

像这样:

class myOperation2 : Operation {

    override func main() {

        print("op2 () working....")

        if self.isCancelled {
            return
        }

        for i in 1...10 {
            print("")
        }
    }
}

取消:

DispatchQueue.global().asyncAfter(deadline: .now()) {
    opsQue.operations[1].cancel()
}