Actually, I can’t determine what exactly is the secret of why your code is not working properly, but I decided that you will find 3 workarounds to achieve what you are trying:
If you expect the output should always be:
op1 working....
op1 finished
op2 working....
op2 finished
then
1 - , , :
class myOperation1 : Operation {
override func main() {
print("op1 working....")
}
}
class myOperation2 : Operation {
override func main() {
print("op2 working....")
}
}
let opsQue = OperationQueue()
let op1 = myOperation1()
op1.completionBlock = {
print("op1 finished")
opsQue.addOperation(op2)
}
let op2 = myOperation2()
op2.completionBlock = {
print("op2 finished")
}
opsQue.addOperation(op1)
2 - maxConcurrentOperationCount 1, :
class myOperation1 : Operation {
override func main() {
print("op1 working....")
}
}
class myOperation2 : Operation {
override func main() {
print("op2 working....")
}
}
let op1 = myOperation1()
let op2 = myOperation2()
op1.completionBlock = {
print("op1 finished")
}
op2.completionBlock = {
print("op2 finished")
}
op2.addDependency(op1)
let opsQue = OperationQueue()
opsQue.maxConcurrentOperationCount = 1
opsQue.addOperation(op1)
opsQue.addOperation(op2)
3 - waitUntilAllOperationsAreFinished() :
let opsQue = OperationQueue()
opsQue.addOperation(op1)
opsQue.waitUntilAllOperationsAreFinished()
opsQue.addOperation(op2)
btw, GCD.
, .