Usage of IOS Dispatch_async

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.