Does the dispatch_sync (dispatch_get_global_queue ()) call in the main thread cause the application to hang?

// Method called when a button is clicked - (void)handleClickEvent { dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self backgroundProcessing]; }); // Some code to update the UI of the view .... [self updateUI]; .... } 

1) handleClickEvent is called in the main thread when a button is clicked on the view.

2) I used dispatch_sync () because the following code, which updates the view user interface, cannot be executed until the variable in the backgroundProcessing method is evaluated.

3) I used dispatch_get_global_queue to get backgroundProcessing from the main thread. (according to the rule: usually put the background processing from the main thread and usually put the code that affects the user interface in the main thread).

My question is: the backgroundProcessing method hangs with the main thread until it is complete, since I'm using dispatch_sync?

EDIT: Based on the answer below, I implemented this solution:

 - (void)handleClickEvent { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self backgroundProcessing]; dispatch_async(dispatch_get_main_queue(), ^{ [self updateUI]; }); }); } 

this link: End calls

+4
source share
1 answer

Yes, dispatch_sync will block until the task completes. Use dispatch_async, and when the job is complete, send the block back to the main queue to update the view.

+7
source

Source: https://habr.com/ru/post/1389982/


All Articles