Common GCD methods and common GCD Methods
// 1. Create a main thread (Serial)
Dispatch_async (dispatch_get_main_queue (), ^ {
// Refresh the interface code
});
// 2. Create an asynchronous thread (parallel)
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Put the time-consuming code here
});
// 3. gcd latency
Double delayInSeconds = 1.0;
Dispatch_time_t popTime = dispatch_time (DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC );
Dispatch_after (popTime, dispatch_get_main_queue (), ^ (void ){
// Delay code
});
// 4. gcd is executed only once
Static dispatch_once_t onceToken;
Dispatch_once (& onceToken, ^ {
// Execute the Code only once
});
// 5. There are three tasks. The first two tasks need to be executed asynchronously and concurrently. The first two tasks are executed before the third task is executed.
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Create a group
Dispatch_group_t group = dispatch_group_create ();
// Associate a task with a group
Dispatch_group_async (group, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Task 1
NSLog (@ "******** execute Task 1 ******");
});
// Associate a task with a group
Dispatch_group_async (group, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Task 2
NSLog (@ "******** execute Task 2 ******");
});
// Wait until the tasks in the group are completed and return to the main thread for block callback.
Dispatch_group_policy (group, dispatch_get_main_queue (), ^ {
// Task 3
NSLog (@ "******* wait for the tasks in the group to be completed, return to the main thread to execute block callback, and execute Task 3 ******");
});
});
// 6. When dispatch_barrier_async is used, dispatch_barrier_async is executed only after the previous task is executed, and the subsequent task will be executed only after it is executed.
Dispatch_queue_t queue = dispatch_queue_create ("create_asy_queue", DISPATCH_QUEUE_CONCURRENT );
Dispatch_async (queue, ^ {
NSLog (@ "dispatch_async1 ");
});
Dispatch_async (queue, ^ {
NSLog (@ "dispatch_async2 ");
});
Dispatch_barrier_async (queue, ^ {
NSLog (@ "dispatch_barrier_async ");
Dispatch_async (dispatch_get_main_queue (), ^ {
NSLog (@ "Refresh interface ");
});
});
Dispatch_async (queue, ^ {
[NSThread sleepForTimeInterval: 1];
NSLog (@ "dispatch_async3 ");
});
/* 7. Another use of GCD is to allow programs to run on the background for a long time.
When GCD is not used, when the app is exited by pressing the home key, the app only needs 5 seconds to save or clear resources. However, after using GCD, the app can run for up to 10 minutes in the background for a long time. This time can be used to clear the local cache and send statistics.
The sample code for long-running programs in the background is as follows:
*/
// AppDelegate. h file
@ Property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;
// AppDelegate. m file
-(Void) applicationDidEnterBackground :( UIApplication *) application
{
[Self beingBackgroundUpdateTask];
// Add the code you need to run for a long time
[Self endBackgroundUpdateTask];
}
-(Void) beingBackgroundUpdateTask
{
Self. backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^ {
[Self endBackgroundUpdateTask];
}];
}
-(Void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self. backgroundUpdateTask];
Self. backgroundUpdateTask = UIBackgroundTaskInvalid;
}