In this example, what's the difference between queue.sync
and queue.async
var queue = DispatchQueue(label: "sample", qos: .background)
queue.sync {
//some code
}
queue.async {
//some code
}
In this example, what's the difference between queue.sync
and queue.async
var queue = DispatchQueue(label: "sample", qos: .background)
queue.sync {
//some code
}
queue.async {
//some code
}
Both snippets append a closure to the queue.
But there is one difference.
Synch will wait for the closure to be executed before processing the next line.
So in this case, the print("Hello")
is executed always after the closure.
queue.sync {
//some code
}
print("Hello")
In this case the closure is added to the queue and then the next line is executed. So the print("Hello")
could be executed before the closure.
queue.async {
//some code
}
print("Hello")