Sobes.tech
Junior — Senior

Determining the output order in a Swift program

livecode

Task condition

Consider the following Swift code snippet that uses DispatchQueue for asynchronous and synchronous execution of tasks on the main queue. What order of numbers will be printed to the console?

func showNumbers() {
    print(1)
    DispatchQueue.main.async {
        print(2)
        DispatchQueue.main.async {
            print(3)
            DispatchQueue.main.sync {
                print(4)
            }
        }
        print(5)
    }
    print(6)
}
print(7)