dispatch_queue_t nameOfQueue = dispatch_queue_create("SOME_NAME", NULL);
dispatch_async(nameOfQueue, ^{
/* codes to be performed in the other queue */
/* eg. downloading something */
dispatch_async(dispatch_get_main_queue(), ^{
/* codes to be performed in main thread, usually UIKit stuff */
/* e.g. update the UI based on the data you have downloaded */
});
});
Note:
- the NULL in dispatch_queue_create means this is a serial queue, as opposed to a concurrent queue
- dispatch_get_main_queue() is used to get the main queue
- dispatch_get_current_queue() is used to get the current queue
It is important to dispatch back to the main queue if you want to run UIKit stuff as they are mostly not thread safe. This is what the second dispatch_async() is doing above. A few exceptions are: UIImage, UIFont, UIBezierPath, drawing things using Core Graphics. (Not sure if there are others.)
See the Apple Documentation.