Explain the use of GCD in IOS

  • 2020-05-15 02:11:35
  • OfStack

Grand Central Dispatch (GCD) is one of the techniques for performing tasks asynchronously. Generally, the code for thread management described in the application is implemented at the system level. Developers simply define the tasks they want to perform and append them to the appropriate Dispatch Queue, and GCD generates the necessary threads and plans to execute the tasks. Because thread management is implemented as part of the system, it can be managed and executed, making it more efficient than previous threads.

1. GCD is a solution proposed by apple for multi-core parallel computing

GCD will automatically take advantage of more CPU cores (such as dual-core and 4-core)

GCD automatically manages the thread lifecycle (creating threads, scheduling tasks, destroying threads)

The programmer just needs to tell GCD what task he wants to perform without writing any thread management code

Process: that is, a running application.

Thread: a complete execution path in a process. A process can have multiple threads, at least one thread, the main thread. In iOS development, all references to the UI interface must be updated in the main thread.

2. How GCD works: line up programs in parallel, scheduling them to perform tasks on any available processor based on available processing resources

3. Serial queue, parallel queue, synchronous task, asynchronous task. Asynchronous tasks open threads: asynchronous tasks open only one child thread in the serial queue, and asynchronous tasks open multiple child threads in the parallel queue.


//GCD  Parallel queues, asynchronous tasks : Start multiple threads and execute at the same time. 
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
for (int i =0; i<1000; i++) {
NSLog(@"AAAAA %d",i);
}
});
dispatch_async(queue, ^{
for (int i =0; i<1000; i++) {
NSLog(@"BBBBB %d",i);
}
});
dispatch_async(queue, ^{
for (int i =0; i<1000; i++) {
NSLog(@"CCCCC %d",i);
}
});
// Serial queue, synchronous task  : Do not start the thread, in order to execute 
dispatch_queue_t Cqueue = dispatch_queue_create("queueName", NULL);
dispatch_sync(Cqueue, ^{
for (int i =0; i<1000; i++) {
NSLog(@"DDDDD %d",i);
}
});
dispatch_sync(Cqueue, ^{
for (int i =0; i<1000; i++) {
NSLog(@"EEEEE %d",i);
}
});
dispatch_sync(Cqueue, ^{
for (int i =0; i<1000; i++) {
NSLog(@"FFFFF %d",i);
}
});

4. When we use multi-threading at ordinary times, we often encounter one requirement: after the completion of the task processing of the child thread, the main thread needs to update UI. How do we know that the child thread is done.

When we use a serial queue, we simply add the last callback main thread task at the end of all the tasks and execute it sequentially. But when we use parallel queues, we don't know when all the child threads are done,

At this point, dispatch_group is required. The code is as follows:


// We're doing several asynchronous tasks at once, and they're done, and sometimes we need to know 1 Next, how to do, this time, will have to use dispatch_group So, I'm going to write the code as follows, and I'm going to do it at the end  dispatch_group_notify  Can be 
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@" Start to perform 1");
});
dispatch_group_async(group, queue, ^{
NSLog(@" Start to perform 2");
});
dispatch_group_async(group, queue, ^{
NSLog(@" Start to perform 3");
});
dispatch_group_notify(group, queue, ^{
NSLog(@" All execution is complete and must be updated on the main thread UI!!!");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@" Main thread update UI To complete. ");
});
});

The above content is this site for you to introduce the use of IOS GCD, I hope to help you!


Related articles: