Our common GCD scheduling group approach
1 //gcd Common Dispatching group2-(void) demo1{3 4 //create dispatch groups and queues5dispatch_group_t Group =dispatch_group_create ();6dispatch_queue_t queue = Dispatch_queue_create ("Changyong", dispatch_queue_concurrent);7 8 //to add a task to a dispatch group9Dispatch_group_async (group, queue, ^{TenNSLog (@"time-consuming operation _11111111"); One }); ADispatch_group_async (group, queue, ^{ - //simulate time-consuming operations -[Nsthread Sleepfortimeinterval:2]; theNSLog (@"time-consuming operation _22222222"); - }); -Dispatch_group_async (group, queue, ^{ -NSLog (@"time-consuming operation _33333333"); + }); - + //tasks performed after all tasks have been completed ADispatch_group_notify (Group, Dispatch_get_main_queue (), ^{ atNSLog (@"All tasks complete, update UI actions"); - }); -}
GCD Scheduling Group principle
1 //principle and method of GCD dispatching group2-(void) demo2{3 4 //create dispatch groups and queues5dispatch_group_t Group =dispatch_group_create ();6dispatch_queue_t queue = Dispatch_queue_create ("Yuanli", dispatch_queue_concurrent);7 8 //Task 19 Dispatch_group_enter (group);TenDispatch_async (Queue, ^{ OneNSLog (@"Task 1"); A Dispatch_group_leave (group); - }); - the //Task 2 - Dispatch_group_enter (group); -Dispatch_async (Queue, ^{ -[Nsthread Sleepfortimeinterval:2]; +NSLog (@"Task 2"); - Dispatch_group_leave (group); + }); A at //Task 3 - Dispatch_group_enter (group); -Dispatch_async (Queue, ^{ -NSLog (@"Task 3"); - Dispatch_group_leave (group); - }); in - //All Tasks completed toDispatch_group_notify (Group, Dispatch_get_main_queue (), ^{ + -NSLog (@"The task finishes, refreshes the UI"); the }); * $ //wait for the above code to execute after executing the following code, Parameter 2: Wait Time outPanax Notoginseng //dispatch_group_wait (Group, dispatch_time_forever); - theNSLog (@"Hello"); +}
The principle of multi-threaded CGD dispatching group