Grand Central Dispatch (GCD) is one of the techniques for asynchronous execution of tasks
The dispatch queue is divided into the following three types:
1) Main queue running in the main thread, and get it through dispatch_get_main_queue.
/*! * @function dispatch_get_main_queue * * @abstract * Returns the default queue that is bound to the main thread. * * @discussion * In order to invoke blocks submitted to the main queue, the application must * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main * thread. * * @result * Returns the main queue. This queue is created automatically on behalf of * the main thread before main() is called. */ __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) DISPATCH_EXPORT struct dispatch_queue_s _dispatch_main_q; #define dispatch_get_main_queue() \ DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q)
It can be seen that dispatch_get_main_queue is also a dispatch_queue_t.
2) The parallel queue global dispatch queue is obtained through dispatch_get_global_queue, and the system creates three dispatch queues with different priorities. The execution order of a parallel queue is the same as the order it joins to the queue.
3) Serial queues are generally used for synchronous access in sequence. They can create any number of serial queues, and each serial queue is concurrent.
Serial queues are useful when you want tasks to be executed in a certain order. The serial queue performs only one task at the same time. We can use serial queues instead of locks to protect shared data. Unlike locks, a serial queue ensures that tasks are executed in a predictable order.
serial queues are created by dispatch_queue_create, and the functions dispatch_retain and dispatch_release can be used to increase or decrease the reference count.
How to use GCD:
// Background execution: dispatch_async(dispatch_get_global_queue(0, 0), ^{ // something }); // Main thread execution: dispatch_async(dispatch_get_main_queue(), ^{ // something }); // Execute at one time: static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // code to be executed once }); // Delay 2 seconds to execute: double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // code to be executed on the main queue after delay }); // Custom dispatch_queue_t dispatch_queue_t urls_queue = dispatch_queue_create("", NULL); dispatch_async(urls_queue, ^{ // your code }); dispatch_release(urls_queue); // Merge summary results dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{ // Thread execution in parallel }); dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{ // Thread 2 that executes in parallel }); dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{ // Summary of results });
An example of applying GCD:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL * url = [NSURL URLWithString:@""]; NSError * error; NSString * data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (data != nil) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"call back, the data is: %@", data); }); } else { NSLog(@"error when download:%@", error); } });
Another use of GCD is that it allows the program to run in the background for a longer period of time.
When GCD is not used, when the app is pressed to exit, the app only needs to save or clean up resources for up to 5 seconds. However, after using GCD, the app will last for up to 10 minutes in the background for a long time. This time can be used to clean up local caches, send statistics, etc.
The example code for making the program run in the background for a long time is as follows:
// document@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask; // document- (void)applicationDidEnterBackground:(UIApplication *)application { [self beingBackgroundUpdateTask]; // Add code you need to run for a long time here [self endBackgroundUpdateTask]; } - (void)beingBackgroundUpdateTask { = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self endBackgroundUpdateTask]; }]; } - (void)endBackgroundUpdateTask { [[UIApplication sharedApplication] endBackgroundTask: ]; = UIBackgroundTaskInvalid; }
The above content is the use of GCD in IOS introduced to you by the editor. I hope it will be helpful to you!