For learning purposes, declared a DispatchQueue inside the demo project.
When I tap the button, the "Statement 1" is printed in the console and after a few seconds the application crashes with the error:
Thread 8: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
I know that asynchronous means that the two things don't know or care when the other is happening. Sync implies that two things must occur at the same time, or alternatively, that one of them must wait until the other one catches up.
Can anyone explain why the app crashed?
@IBAction func queuePrint(_ sender: UIButton) {
let queue = DispatchQueue(label: "io.myQueue.queue")
queue.async {
print("Statement 1")
queue.sync {
print("Statement 2")
queue.async {
print("Statement 3")
}
}
}
queue.async {
print("Statement 4")
}
queue.async {
print("Statement 5")
}
}