Cancel

operation.cancel()
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 {
            if self.isCancelled { 
                break
            } 
            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()
}

op1 (🐭) working....
op3 (🍉) working....
op2 (🐶) working....
🐭
🍉
op2 (🐶) completed
🐭
🍉
🍉
🐭

isAsynchronous

직접 실행 시켜줄 경우

OperationQueue에 넣어서 작업을 처리하게 하면