Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Dispatching a block to the main queue is usually done from a background queue to signal that some background processing has finished e.g.</p> <pre><code>- (void)doCalculation { //you can use any string instead "com.mycompany.myqueue" dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0); dispatch_async(backgroundQueue, ^{ int result = &lt;some really long calculation that takes seconds to complete&gt;; dispatch_async(dispatch_get_main_queue(), ^{ [self updateMyUIWithResult:result]; }); }); } </code></pre> <p>In this case, we are doing a lengthy calculation on a background queue and need to update our UI when the calculation is complete. Updating UI normally has to be done from the main queue so we 'signal' back to the main queue using a second nested dispatch_async.</p> <p>There are probably other examples where you might want to dispatch back to the main queue but it is generally done in this way i.e. nested from within a block dispatched to a background queue.</p> <ul> <li>background processing finished -> update UI</li> <li>chunk of data processed on background queue -> signal main queue to start next chunk</li> <li>incoming network data on background queue -> signal main queue that message has arrived</li> <li>etc etc</li> </ul> <p>As to why you might want to dispatch to the main queue <em>from</em> the main queue... Well, you generally wouldn't although conceivably you might do it to schedule some work to do the next time around the run loop.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload