dispatch_get_global_queue vs dispatch_get_main_queue

The dispatch_get_global_queue (DispatchQueue.global() in Swift) gets you a background queue upon which you can dispatch background tasks that are run asynchronously (i.e. won't block your user interface). And if you end up submitting multiple blocks to the global queues, these jobs can operate concurrently. If you have multiple blocks of code that you want to submit to a background queue that you must have run sequentially in the background (not often needed), you could create your own serial background queue and dispatch to that, but if concurrent background operations are acceptable, then availing yourself of dispatch_get_global_queue is convenient/efficient.

Be aware, though, that you're not allowed to perform user interface updates in the background queue, so the dispatch_async to the dispatch_get_main_queue (i.e. DispatchQueue.main.async { ... } in Swift) lets that background queue dispatch the user interface updates back to the main queue, once the main queue is available.

This is a very common programming pattern: Submit something to run in the background and when it needs to perform user updates, dispatch the update back to the main queue.

For more information, refer to the Concurrency Programming Guide.


**dispatch_get_main_queue** :- Perform UI updates on this queue

 dispatch_async(dispatch_get_main_queue(), ^{


self.label.text=@"Hello";

    });

**dispatch_get_main_queue**:- Perform background tasks like downloading content

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    for (int i=0; i<100000;enter code here i++) {
        NSLog(@"HIGH 1 %d", i);
    }
});

Global queue gives you a queue other than the main que but saves you the trouble of actually creating your own queue. Use get_main_queue when you need your code to work on the main queue (where all of your UI work needs to happen)


The dispatch_get_main_queue should be used anytime you want to manipulate UI elements. This has to do with thread affinity, a common model for UI frameworks. Thread affinity means you can only manipulate the object on the thread on which that object was created. For UI classes in Cocoa Touch, that's the main thread. This is a typical idiom for UI frameworks on all platforms that matter.

So dispatch_get_main_queue gets the queue associated with the main thread. Not doing this causes weird stuff to happen when your UI is updated on a different thread. I typically see long pauses where the UI freezes.

dispatch_get_global_queue gets any old queue of the given priority level associated with your app. Perfect for network calls or, as in your case, working with Core Data.