DispatchQueue.main.async {} in viewDidLoad

Viewed 5846

I was wondering what happens if I call something asynchronously in main queue from viewDidLoad method. A little experiment showed me these results:

This code:

override func viewDidLoad() {
    super.viewDidLoad()

    firstSelector()
    DispatchQueue.main.async {
        self.secondSelector()
    }
    for i in 1...10 {
        print(i)
    }
    thirdSelector()

}

func firstSelector() {
    print("First selector fired")
}

func secondSelector() {
    print("Second selector fired")
}

func thirdSelector() {
    print("Third selector fired")
}

Gives these prints:

First selector fired
1
2
3
4
5
6
7
8
9
10
Third selector fired
Second selector fired

So the last one method that was called is secondSelector. I think this is because main queue is serial and when I call asynchronously some method (secondSelector in this case) it returns immediately and waits until all other methods will be completed. After queue is free of tasks it completes method that I called asynchronously. Am I right in my thoughts?

1 Answers
Related