Your code basically executes like this:
// 1. Send this code off to the global queue to be processed async
DispatchQueue.global(qos: .background).async {
// 3. You're here once the global queue decides it's ready to run this code
// 4. Send this code off to the main queue to be processed async
DispatchQueue.main.async {
// 6. You're here once the main queue decides it's ready to run this code
print("This is run on the main queue, after the previous code in outer block")
}
// 5. This is now printed
print("This is run on the background queue")
}
// 2. whatever happens here
The reason your code runs like this is that you're dispatching everything asynchronously. this means that all you do is pass the closure off to be executed by the targetted queue at a later time, or whenever it's ready. By using async you are telling the queue you don't want to wait for this.
If you'd want your main queue bit to run immediately, you can use DispatchQueue.main.sync
instead. That will block the execution of the context you're in (in this case the async
closure you passed to the global queue) until the closure you're running with sync
is completed.
I would typically recommend to avoid sync
unless you really need it because it's far too easy to make a queue wait for itself and be locked forever (deadlock).