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, recall the previous multithreaded programming, we will put the code of the asynchronous call into another function, and through the Nsthread to open a new thread to start the code. 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 approach, as shown in the code:
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. 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.
Hope can be helpful for beginners to understand deeply!
Usage of IOS Dispatch_async