简体   繁体   English

viewDidLoad中的DispatchQueue.main.async {}

[英]DispatchQueue.main.async {} in viewDidLoad

I was wondering what happens if I call something asynchronously in main queue from viewDidLoad method. 我想知道如果我从viewDidLoad方法在主队列中异步调用某些内容会发生什么。 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. 所以最后一个被调用的方法是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. 我认为这是因为主队列是串行的,当我异步调用某个方法(在本例中为secondSelector)时,它立即返回并等待直到所有其他方法都完成。 After queue is free of tasks it completes method that I called asynchronously. 在队列没有任务之后,它完成了我异步调用的方法。 Am I right in my thoughts? 我的想法是对的吗?

I used to ask a similar question . 我曾经问过类似的问题 Let me quote the important part of the answer I got: 让我引用我得到的答案的重要部分:

"Because the default runloop on the main thread has the special behaviour that, when run, it also processes for the main dispatch queue..." “因为主线程上的默认runloop具有特殊的行为,在运行时,它还会处理主调度队列...”

When you do dispatch_async to a main thread, your block { self.secondSelector() } gets scheduled to a main run loop. 当您对主线程执行dispatch_async ,您的块{ self.secondSelector() }将被调度到主运行循环。 Since viewDidLoad method is already being executed by the main run loop, your dispatched block with be processed after viewDidLoad and all other (possible) blocks or methods that were scheduled before your block will be executed. 由于viewDidLoad方法已由主运行循环执行,因此您的调度块将在viewDidLoad之后处理,并且所有其他(可能的)块或方法将在块之前执行。

Keep in mind that your question is about behaviour of dispatch_async when you dispatch to the main queue and main run loop from the main thread. 请记住,当您从主线程调度到主队列和主运行循环时,您的问题是关于dispatch_async行为。 viewDidLoad has nothing to do with it - the only thing where it is relevant here, is that UIViewController 's lifecycle methods like viewDidLoad , viewWillAppear etc are all run on the main thread (processed by a main run loop). viewDidLoad与它无关 - 这里唯一与之相关的是, UIViewController的生命周期方法如viewDidLoadviewWillAppear等都在主线程上运行(由主运行循环处理)。 You will see the same behaviour with any method other than viewDidLoad given this method is run on a main thread. 鉴于此方法在主线程上运行,您将看到与viewDidLoad之外的任何方法相同的行为。 If you call dispatch_async from other thread you might be surprised by different results because in that case you will have two threads working at the same time (your other thread and a main thread to which you dispatched). 如果从其他线程调用dispatch_async,您可能会对不同的结果感到惊讶,因为在这种情况下,您将有两个线程同时工作(您的另一个线程和您调度的主线程)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM