A previous article on the thread of the blog has introduced the simple use of gcd and examples, today, because the project has a special application GCD examples, for everyone to introduce two special needs of the use of GCD method.
Objective: to accomplish one thing, and then do the next, to ensure the function execution cycle.
Solution: The following 2 solutions are available
1. Create barrier waiting thread
Dispatch_async (Dispatch_get_main_queue (), ^{
[self actionfirst];
});
Dispatch_barrier_async (Dispatch_get_main_queue (), ^{
[self actionnext];
});
2. Create group thread groups (that is, queue queues)
dispatch_group_t group = Dispatch_group_create ();
Dispatch_group_async (Group, Dispatch_get_main_queue (), ^{
[self actionfirst];
});
Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{
[self actionnext];
});
The code for the problem appears as follows:
-(void) Example
{
[Self actionfirst];
[Self actionnext];
}
cause : When calling the example function, because the Actionfirst function executes longer, Actionfirst may not have finished executing when the Actionnext function finishes executing, Causes a crash when executing actionnext.
Advanced usage of IOS thread GCD