简体   繁体   中英

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? 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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