In the previous article, we talked about the basic gcd, as well as synchronous, asynchronous, concurrent, parallel to the understanding of several probabilities.
Reference Links:
IOS GCD Basics-synchronous, asynchronous, concurrency, parallel understanding
Now let's talk about the concept of dispatch_group and the use of several scenarios.
1, about Dispatch_group
Submits a set of tasks to the queue, which can be irrelevant, and then listen for events completed by this set of tasks.
Some of the most common methods:
1. Dispatch_group_create Create a dispatch task group
2. Dispatch_group_async to commit a task asynchronously to a task group
3, Dispatch_group_enter/dispatch_group_leave This method is used to submit a task without using Dispatch_group_async, and must be used in conjunction with
4, dispatch_group_notify used to monitor the task group event execution completed
5, Dispatch_group_wait Set the wait time, after the wait time is over, if the task group has not finished executing, then return. Returns 0 for execution success, not 0 for execution failure
2. Actual use of the scene
Scenario 1:
There are now 4 tasks, Task 1, Task 2, Task 3, Task 4. Task 3 must be after Task 2, task 4 must be completed before the first 3 tasks are executed, and the UI needs to be updated on the main thread.
Thinking Analysis:
Task 3 must be performed after Task 2, so both must be serially executed, while tasks 2 and 3 can be executed in parallel with Task 1, and finally, Task 4 can only wait for the first 3 tasks to complete before it can be executed. This allows you to quickly implement scenario requirements with group.
Code implementation:
-(void) disgroup{ dispatch_queue_t globalquene = dispatch_get_global_queue (dispatch_queue_priority_default, 0); dispatch_queue_t Selfquene = dispatch_queue_create ("Myquene", 0); dispatch_group_t group = Dispatch_group_create (); Dispatch_group_async (Group, Globalquene, ^{ NSLog (@ "Run Task 1"); }); Dispatch_group_async (Group, Selfquene, ^{ NSLog (@ "Run Task 2"); }); Dispatch_group_async (Group, Selfquene, ^{ NSLog (@ "Run task 3"); }); Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{ NSLog (@ "Run Task 4");} );
The execution results are as follows: (Result 1 and result 2 are the same, only 1 and 2 are the order difference)
Results 1
Or
Results 2
Summary: 1 and (2, 3) is a parallel execution relationship, 2, 3 is a serial execution relationship, and 3 must be after 2, and 4 will be executed after all (1, 2, 3) is complete.
2, Dispatch_group_enter(group),Dispatch_group_leave(group) when to use
In theory, these two methods are actually: Manually manage the running state (or count) of the block associated with the group, and must be guaranteed to enter and exit the group number match when used.
So: both A and b can be used in the same way.
A) Dispatch_group_async (group, queue, ^{//...) }); B) Dispatch_group_enter (group);d Ispatch_async (queue, ^{//... Dispatch_group_leave (group);});
Therefore, this usage and direct use of dispatch_group_notify to some extent is equivalent, you can choose their own use.
Scenario 2:
There are 3 asynchronous request tasks, Tasks 1, 2, 3, and after all 3 tasks are completed, Task 4 needs to be performed to display the interface data.
Implemented with Dispatch_group_enter and Dispatch_group_leave:
-(void) disgroupenterandleave{ dispatch_queue_t globalquene = Dispatch_get_global_queue (dispatch_queue_ Priority_default, 0); dispatch_group_t group = Dispatch_group_create (); Task 1 dispatch_group_enter (group); Dispatch_async (Globalquene, ^{ NSLog (@ "Run Task 1"); Sleep (1); Dispatch_group_leave (group); }); Task 2 dispatch_group_enter (group); Dispatch_async (Globalquene, ^{ NSLog (@ "Run Task 2"); Sleep (2); Dispatch_group_leave (group); }); Task 3 dispatch_group_enter (group); Dispatch_async (Globalquene, ^{ NSLog (@ "Run task 3"); Sleep (3); Dispatch_group_leave (group); }); have been waiting to complete dispatch_group_wait (group, dispatch_time_forever); Task 3 dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{ NSLog (@ "Run Task 4");} );
Execution Result:
3 tasks are executed in parallel and take a total of 3s execution before performing task 4.
Of course, if you want to perform 3 tasks serially, you only need to change the global queue to a custom queue .
IOS GCD Intermediate-dispatch_group understanding and use