Advanced usage of GCD for iOS threads
A previous blog on threads has introduced the simple usage and example of GCD. Today, due to the special application of GCD in the project, the following describes how to use GCD with two special requirements.
Purpose: To finish one task, and then perform the next task to ensure the function execution cycle.
Solution: provides the following two solutions:
1. Create a barrier wait thread
Dispatch_async (dispatch_get_main_queue (), ^ {
[Self ActionFirst];
});
Dispatch_barrier_async (dispatch_get_main_queue (), ^ {
[Self ActionNext];
});
2. Create a group thread group (queue)
Dispatch_group_t group = dispatch_group_create ();
Dispatch_group_async (group, dispatch_get_main_queue (), ^ {
[Self ActionFirst];
});
Dispatch_group_policy (group, dispatch_get_main_queue (), ^ {
[Self ActionNext];
});
The error code is as follows:
-(Void) example
{
[Self ActionFirst];
[Self ActionNext];
}
Cause: When the example function is called, because the ActionFirst function has been executed for a long time, when the ActionNext function is executed, the ActionFirst function may not be executed, resulting in a crash when the ActionNext function is executed.