dispatch your tasks on dispatch_get_main_queue() for UI changes.
The main queue is a special serial queue. Unlike other serial queues, which are uncommitted, in that they are “dating” many threads but only one at time, the main queue is “married” to the main thread and all tasks are performed on it. Jobs on the main queue need to behave nicely with the runloop so that small operations don’t block the UI and other important bits. Like all serial queues, tasks are completed in FIFO order. You get it with dispatch_get_main_queue.
1 2 3 |
dispatch_async(dispatch_get_main_queue(), ^{ //UI updates here }]; |
dispatch your tasks on dispatch_get_global_queue (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.
NOTE THAT 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, you could create your own serial background queue
1 |
dispatch_queue_t serialQueue = dispatch_queue_create(DISPATCH_QUEUE_SERIAL,0); |
and dispatch to that.
Hence dispatch_get_global_queue is concurrent in nature.
1 2 3 4 5 6 7 8 9 |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ //CODE THAT DOES NON-UI WORK //when you are done and want to update to UI do this: dispatch_async(dispatch_get_main_queue(), ^{ }]; }]; |