Usage and function of dispatch_apply in GCD, gcddispatch_apply
Usage and function of dispatch_apply in GCD
(1) Basic usage of dispatch_apply
The dispatch_apply function is the Association API between the dispatch_sync function and the Dispatch Group function. This function appends the specified Block to the specified Dispatch Queue Based on the specified number of times, and waits until all processing is completed.
1 /*! 2 * @ brief dispatch_apply usage 3 */4-(void) dispatchApplyTest1 {5 // generate global queue 6 dispatch_queue_t queue = dispatch_get_global_queue (queue, 0); 7 8 9 /*! Description of the dispatch_apply function 10*11 * @ brief dispatch_apply function is the association between the dispatch_sync function and the Dispatch Group API12 *. This function appends the specified Block to the specified Dispatch Queue Based on the specified number of times, wait until all processing operations are completed 13*14 * @ param 10 specify the number of repetitions specify 10 times 15 * @ param queue append the Dispatch Queue16 * @ param index with a parameter Block, index is used to differentiate Block17 * 18 */19 dispatch_apply (10, queue, ^ (size_t index) {20 NSLog (@ "% zu ", index); 21}); 22 NSLog (@ "done"); 23 24 /*! 25 * @ brief output result 26*27 19:24:39. 102 dispatch_apply test [2985: 165004] 028 19:24:39. 102 dispatch_apply test [2985: 165086] 129 19:24:39. 104 dispatch_apply test [2985: 165004] 430 19:24:39. 104 dispatch_apply test [2985: 165004] 531 19:24:39. 104 dispatch_apply test [2985: 165004] 632 19:24:39. 103 dispatch_apply test [2985: 165088] 333 19:24:39. 104 dispat Ch_apply test [2985: 165004] 734 19:24:39. 105 dispatch_apply test [2985: 165004] 835 19:24:39. 105 dispatch_apply test [2985: 165004] 936 19:24:39. 102 dispatch_apply test [2985: 165087] 237 19:24:39. 105 dispatch_apply test [2985: 165004] done38 *!!! Because it is executed in the Global Dispatch Queue, the execution time of each processing is not limited to 39, but the done will be output at the last position, because the dispatch_apply function will wait for the processing to end 40 */41}
(2) How to Use dispatch_apply: simulate a for Loop
1 /*! 2 * @ brief instance: When you want to process all elements of the NSArray class object, you do not have to write the for loop section 3 */4-(void) one by one) dispatchApplyTest2 {5 // 1. create NSArray Class Object 6 NSArray * array = @ [@ "a", @ "B", @ "c", @ "d", @ "e ", @ "f", @ "g", @ "h", @ "I", @ "j"]; 7 8 // 2. create a global queue 9 dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 10 11 // 3. use the dispatch_apply function to process all elements in NSArray and wait until the processing is completed. 12 dispatch_apply ([array count], queue, ^ (size _ T index) {13 NSLog (@ "% zu: % @", index, [array objectAtIndex: index]); 14}); 15 NSLog (@ "done "); 16 /*! 17 * @ brief output result 18*19 19:37:17. 308 dispatch_apply test [3010: 167871] 0: a20 19:37:17. 308 dispatch_apply test [3010: 167956] 1: b21 19:37:17. 308 dispatch_apply test [3010: 167957] 3: d22 19:37:17. 308 dispatch_apply test [3010: 167871] 4: e23 19:37:17. 309 dispatch_apply test [3010: 167957] 6: g24 19:37:17. 309 dispatch_apply test [3010: 167871] 7: h25 1 9:37:17. 309 dispatch_apply test [3010: 167957] 8: i26 19:37:17. 309 dispatch_apply test [3010: 167871] 9: j27 19:37:17. 308 dispatch_apply test [3010: 167956] 5: f28 19:37:17. 308 dispatch_apply test [3010: 167955] 2: c29 *!!! Because it is executed in the Global Dispatch Queue, the execution time of each processing is not set to 30, but the done will be output at the last position, because the dispatch_apply function will wait until the processing ends 31 */32}
(3) execute the dispatch_apply function asynchronously in the dispatch_async function to simulate the synchronization effect of dispatch_sync.
1 /*! 2 * @ brief we recommend that you asynchronously execute the dispatch_apply function in the dispatch_async function. 3. The effect of the dispatch_apply function is the same as that of the dispatch_sync function. The execution will wait until the execution ends) dispatchApplyTest3 {6 NSArray * array = @ [@ "a", @ "B", @ "c", @ "d", @ "e", @ "f ", @ "g", @ "h", @ "I", @ "j"]; 7 dispatch_queue_t queue = dispatch_get_global_queue (queue, 0); 8 9 dispatch_async (queue, ^ {10 11 dispatch_apply ([array count], queue, ^ (size_t index) {12 NSLog (@ "% zu: % @", index, [array objectAtIndex: index]); 13}); 14 15 dispatch_async (dispatch_get_main_queue (), ^ {16 NSLog (@ "back to the main thread to execute User Interface updates and other operations"); 17}); 18 19}); 20 /*! 21 * @ brief execution result 22*23 19:49:53. 189 dispatch_apply test [3060: 171856] 3: d24 19:49:53. 189 dispatch_apply test [3060: 171852] 1: b25 19:49:53. 189 dispatch_apply test [3060: 171853] 2: c26 19:49:53. 189 dispatch_apply test [3060: 171850] 0: a27 19:49:53. 189 dispatch_apply test [3060: 171856] 4: e28 19:49:53. 189 dispatch_apply test [3060: 171852] 5: f29 19:49:53. 190 dispatch_apply test [3060: 171853] 6: g30 19:49:53. 190 dispatch_apply test [3060: 171850] 7: h31 19:49:53. 190 dispatch_apply test [3060: 171852] 9: j32 19:49:53. 190 dispatch_apply test [3060: 171856] 8: i33 19:49:53. 218 dispatch_apply test [3060: 171760] return to the main thread to perform operations such as user interface update 34*35 */36}