iOS Getting the Main Queue


Example

The main queue is the dispatch queue in which all the UI updates take place and the code involving UI changes are placed.

You need to get to the main queue in order to update UI on completion of an asynchronous process like NSURLSession

There are two types of main queue calls synchronous and asynchronous. When you invoke something synchronously, it means that the thread that initiated that operation will wait for the task to finish before continuing. Asynchronous means that it will not wait.

Code Objective-C

Synchronous Main Queue call

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

Asynchronous Main Queue call

dispatch_async(dispatch_get_main_queue(), ^{
   // do work here to Usually to update the User Interface
});

SWIFT 3

Asynchronous Main Queue call

DispatchQueue.main.async {

}

Synchronous Main Queue call

DispatchQueue.main.sync {

}