简体   繁体   English

从后台队列调用iOS主线程操作可以中断方法调用吗?

[英]Can invoking iOS main thread operation from background queue interrupt a method call?

Lets say I have a background queue that just finished, and at the end it will do some stuff on the main thread: 可以说我有一个刚刚完成的后台队列,最后它将在主线程上做一些事情:

-(void) backgroundThreadFunction {
     //Some stuff on background thread. 
     dispatch_async(dispatch_get_main_queue(), ^{
         [self doSomeNetworkStuff];
     });
}

Lets say in the main thread, there is already a method currently running: 可以说在主线程中,已经有一个正在运行的方法:

-(void)myMainMethod{
     //Running some tasks.
}

Does iOS wait till myMainMethod to complete before allowing doSomeNetworkStuff() to run? iOS是否要等到myMainMethod完成才能允许doSomeNetworkStuff()运行? Or does it allow it to interrupt? 还是允许它中断?

No, it can't interrupt. 不,它不能打扰。 The line dispatch_async(dispatch_get_main_queue(), ^{ adds the block to the main queue. The current method is already running on the main queue. 一行dispatch_async(dispatch_get_main_queue(), ^{将块添加到主队列中。当前方法已在主队列上运行。

It's like standing in line at the bank with one teller. 就像在银行与一个柜员站在一起。 People get in line and wait their turn. 人们排队等候,轮到他们了。 The current customer is not interrupted by the next person in line (let us assume this is a polite line). 当前的客户不会被下一个排队的人打扰(让我们假设这是礼貌的排队)。

Once myMainMethod (the current customer) completes (and the method that called it completes, etc.), then the next block in the queue (the next customer) is processed. 一旦myMainMethod (当前客户)完成(并且调用它的方法也完成,等等),然后将处理队列中的下一个块(下一个客户)。

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

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