GCD (Grand Central Dispatch) is a technology developed by Apple to optimize concurrent operations in multicore environments and replace traditional multithreaded programming patterns. Support for GCD starts after Mac OS X 10.6 and iOS 4.0.
One reason to use GCD is convenience. Recalling the previous multithreaded programming, we will put the code of the asynchronous call into another function, and start the code by nsthread open a new thread. This jumping-and-jump process is a disaster for complex logic. What's worse, the environment when invoking the thread is not visible to async code, and if we need the temporal variable then there are only two choices: Save to a class member variable or pass it as a parameter. The former creates a lot of strange and strange unrelated class members, and the latter is too limited in function.
GCD is relatively a more elegant way to look at the following code:
NSString* parameter = [self getSomeParameter];dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString* result = [self fetchResultFromWebWithParameter:parameter]; dispatch_async(dispatch_get_main_queue(), ^{ [self updateUIWithResult:result]; });});
In the above code, there is a strange format:
^{code...}
Explain that when a piece of code is wrapped in curly braces and placed on the top of the apex, we call it block. If you've learned C (in fact, block is Apple's extension of C), you can think of it as an enhanced function pointer. Not only can it be passed back and forth as a variable, but it can also refer to variables outside of its own environment (such as parameter in the code above). Further, it is the implementation of the closure in Apple's C extension. The objects referenced in the block are automatically retain, so you don't have to worry about memory issues.
There are also three functions involved
void dispatch_async( dispatch_queue_t queue, dispatch_block_t block);dispatch_queue_t dispatch_get_global_queue( long priority, unsigned long flags); dispatch_get_main_queue();
The dispatch_async function will run the incoming block block into the specified queue. This function is asynchronous, which means that it returns immediately regardless of whether or not the block is running at the end. Therefore, we can run various time-consuming operations (such as network requests) in the block without blocking the UI thread.
dispatch_get_global_queue Gets a global queue, and we'll take it as a few global threads that the system opens for us. We use priority to prioritize the queue, and flag as the reserved field (typically 0).
dispatch_get_main_queue Returns the home column, which is the UI queue. It is typically used when some work is done asynchronously in other queues, and the interface needs to be updated in the UI queue (such as [self updateuiwithresult:result]in the code above).
Well, with these features in place, we can understand the code above: asynchronously initiates a network request with the parameter variable and updates the UI thread after the request.
Dispatch_async and Dispatch_get_global_queue