简体   繁体   中英

What is the difference between perform a method directly and in the block of dispatch_async(dispatch_get_main_queue(), ^{})

I find that in some cases, the result that I perform [self doSomething] directly is different from that I perform in the block of a GCD body like this:

dispatch_async(dispatch_get_main_queue(), ^{

    [self doSomething]

}) 

I have perform [NSThread currentThread] to confirm it has already been on the main thread. So what is the difference?

Only [self doSomething] will be synchronized call and using dispatch_async will be asynchronized call.

Statement A
[self doSomething]
Statement B

Above code will start executing Statement A, finish Statement A , Start executing function doSomething , finish function doSomething and then start executing and finish Statement B .

Statement A
dispatch_async(dispatch_get_main_queue(), ^{
    [self doSomething]
})
Statement B

Above block will start and finish execution of Statement A , then it adds doSomething function call in the queue (it may or may not start immediately) then it will start execution of Statement B without waiting to finish execution of function doSomething .

So, if Statement B (and other statements which are after your function call) is independent of result of function doSomething , then you can do async call.

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