简体   繁体   中英

Purpose of running dispatch_async(dispatch_get_main_queue() on UI Thread

It's quite common to see in real cases that dispatch_async(dispatch_get_main_queue() is implemented on UI Thread. I can not figure out its purpose.

My thinking is since there's only one thread on the UI thread, what's the purpose of dispatch_async?

Take a real example I met days ago here:

Run a "NSTimer scheduledTimerWithTimeInterval" on the UI thread to update "_scrollView contentOffset"

1 no dispatch: setContentOffset get effective until next NSTimer schedule

2 with dispatch : setContentOffset get effective immediately

dispatch_async(dispatch_get_main_queue(), ... ) forces a block of code to be run asynchronously on the next run loop of the main thread (UI thread). Some code must be run on the UI thread, such as adding/removing widgets from the UI or updating the UI.

dispatch_async( .. ) is used as to not block the current thread.

dispatch_async(dispatch_get_main_queue(),
                       ^{
                           // perform something on UI
                       });

dispatch_async is a low level GCD API (Grand Central Dispatch API). As we know Applications main thread is used to perform any activities on UI like performing animations/ any other UI related stuff, so in the context of doing some work with the UI it is essential that it should have access to main thread and what above GCD API does it provides the same guaranteed access to the main thread for the UI to perform any related operations.

GCD to the main queue is usually done from a background queue to signal that some background processing has finished. For example

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
    // Here you are doing a very expensive operation which will take let say 5 secs. And after 5 sec you will got your result. Let say

    int output = //calculated result.

    dispatch_async(dispatch_get_main_queue(), ^{
           [[self myLable] setText:[NSString stringWithFormat:@"%d", output]];     
    });
});

In this case, we are doing a lengthy calculation on a background queue to not to block the main thread. And after getting the result we need to update the UI. UIKit should not be accessed from any other thread, it should be accessed from main thread to avoid the race condition and prevent the crashing during accessing UIKit . So here we are using dispatch_async to give a chance to the main thread to work by dispatch_get_main_queue() .

I personally use it when inside another dispatch_async (background, non-UI thread) - when I do something like computation or just anything that can run on a background thread and then I want to "come back" and update the UI accordingly, dispatch_async(dispatch_get_main_queue()...) returns me back on UI thread and thus I can update the UI. Otherwise if I'd stay on the background thread in the first dispatch_async , no changes would be made on the UI.

maybe the code "dispatch_async" is not on main thread, dispatch_get_main_queue guarantee the code in the block runs on main thread.

it likes performselectonmainthread,

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