Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatch_async on main_queue?

Tags:

ios

I have seen this code snippet:

dispatch_async(dispatch_get_main_queue(), ^{     [self doSomeNetworkStuff]; }); 

This doesn't look like making much sense to me.

EDIT: To clarify the conditions of my question:

  • The call to dispatch_async is performed from the main thread.
  • The sent message doSomeNetworkStuff is the heavy lifting worker task.
  • ... and is not only the UI-updating task.

Dispatch, sure, but using the main queue would just pull the dispatched task back to the ui thread and block it.

Please, am I missing something? Thanks.

like image 693
nine stones Avatar asked Mar 02 '13 02:03

nine stones


People also ask

What is Dispatch_async?

Submits a block for asynchronous execution on a dispatch queue and returns immediately.

What is dispatch_ async in iOS?

Submits a block object for execution and returns after that block finishes executing.

What is Dispatch_get_main_queue?

A dispatch object that prioritizes the execution of tasks based on their quality-of-service (QoS) level.

What is Dispatch_get_global_queue?

Returns a system-defined global concurrent queue with the specified quality-of-service class.


1 Answers

dispatch_async lets your app run tasks on many queues, so you can increase performance. But everything that interacts with the UI must be run on the main thread. You can run other tasks that don't relate to the UI outside the main thread to increase performance.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    //Add some method process in global queue - normal for data processing      dispatch_async(dispatch_get_main_queue(), ^(){     //Add method, task you want perform on mainQueue     //Control UIView, IBOutlet all here      });   //Add some method process in global queue - normal for data processing  }); 
like image 155
Alex Avatar answered Sep 19 '22 23:09

Alex