Swift-GCD,
This article introduces the GCD application operations in the Swift3 language. target readers can be beginners without the basis of GCD, you can also have a certain understanding of GCD, but want to have a more comprehensive understanding of developers.
I. GCD Introduction
In iOS, Apple provides two methods for multi-task Programming: Grand Central Dispatch (GCD) and NSOperationQueue. When we need to allocate tasks to different threads or other queues with non-main columns, these two methods can meet the requirements well. Which method is subjective? However, this tutorial only focuses on the previous method, GCD. No matter which method is used, there is a rule that must be kept in mind: no operation can block the main thread and must be used for interface response and user interaction. All time-consuming operations or tasks with high CPU requirements must be executed in the concurrent or backend queue.
GCD was launched in iOS 4. It provides great flexibility and selectivity for concurrency, performance, and parallel tasks. But before Swift 3, it had a great disadvantage: because its programming style is very close to the underlying C, it is very different from the Swift programming style, and the API is hard to remember, it is inconvenient to use in Objective-C. This is the main reason why many developers choose NSOperationQueue instead of using GCD. Simply Baidu, you will be able to understand what the syntax of GCD was like.
In Swift 3, these changes have been greatly changed. Swift 3 adopts the new Swift syntax style to rewrite GCD, which makes it easy for developers to get started. These changes give me the motivation to write this article. Here we mainly introduce the most basic and important knowledge of GCD in Swift 3. If you have used the GCD of the old syntax style (even if you only use one), the new style described here is a piece of cake for you. If you have not used GCD before, then you are about to start a new chapter in programming.
GCD Concept
First, the core term in GCD is dispatch queue. A queue is actually a series of code blocks that can be executed synchronously or asynchronously in the main thread or background thread. Once the queue is created, the operating system takes over the queue and assigns it to any core for processing. No matter how many queues there are, they can be correctly managed by the system, which does not require manual management by developers. The queue follows the FIFO mode (first-in-first-out), which means that tasks in the first queue will be executed first (as you can imagine, the first queue will be served first, at the end of the page, the service will be served ).
Next, another important concept is WorkItem ). A task item is a code block that can be created along with the creation of a queue, encapsulated, and reused in subsequent code. As you think, the task item code is the code that dispatch queue will execute. Task items in the queue also follow the FIFO mode. These operations can be synchronous or asynchronous. In the case of synchronization, the application will keep blocking the current thread until the code execution is complete. During asynchronous execution, the application executes the task item first and returns immediately without waiting for the execution to complete. We will see their differences in the following instances.
After learning about the two concepts above (queue and task items), we need to know that a queue can be serial or parallel. In a serial queue, a task item can be executed only after the previous task item is completed (unless it is the first task item). In a parallel queue, all task items can be executed in parallel.
When adding tasks to the master queue, always be careful. This queue should be used for interface response and user interaction at any time. Remember that all updates related to the user interface must be executed in the main thread. If you try to update the UI in the background thread, the system does not guarantee when the update will occur. In most cases, this will bring a poor user experience. However, all tasks that occur before the interface update can be executed in the background thread. For example, we can download image data from the queue or the background queue, and then update the corresponding image view in the main thread.
We do not need to create our own queue every time. The global queue maintained by the system can be used to execute any tasks we want to execute. As to which thread the queue is running, iOS maintains a thread pool, that is, a series of threads other than the main thread, the system selects at most one thread for use (depending on the data in the queue you created and the method in which the queue is created ). Which thread will be used is unknown to developers. It is determined by the system based on the current concurrent tasks, processor loads, and other conditions ". To be honest, who wants to handle the above work besides the system.
Preliminary understanding
// A tag named "queue0" is created, and the execution priority is. default parallel queue let queue0 = DispatchQueue (label: "queue0", qos :. default, attributes :. concurrent) // attribute explanation: // label: queue label // qos: Set the queue priority // attributes: queue format: Default serial, set. concurrent indicates that there are two parameters in the parallel queue // initialization method, but we seldom use this method to create a simple serial Queue (without setting the priority) let queue1 = DispatchQueue (label: "queue1 ")
- Create a serial queue with a priority
let queue2 = DispatchQueue(label: "queue2", qos: .background)
// No priority is set. let queue3 = DispatchQueue (label: "queue3", attributes :. concurrent) // set the priority let queue4 = DispatchQueue (label: "queue4", qos :. background, attributes :. concurrent)
- Global queue of the system
// System global queue let queue5 = DispatchQueue. global () let queue6 = DispatchQueue. global (qos:. default)
- Main columns of the system
// System main queue column: This column is mainly used to operate the UI page information. Do not add time-consuming tasks to let main = DispatchQueue. main.
GCD serial
- Serial synchronization task
Override func viewDidAppear (_ animated: Bool) {super. viewDidAppear (animated) queueExample ()} func queueExample () {// serial queue let queue1 = DispatchQueue. global () // synchronization task // Task 1 queue1.sync {for I in 1... 10 {print ("