Execute only once (more for singleton mode)
Dispatch_once (dispatch_once_t *predicate, dispatch_block_t block);
dispatch_once_t *predicate: A global variable dispatch_block_t block:block function block
Multiple executions
Dispatch_apply (size_t iterations, dispatch_queue_t queue,void (^block));
size_t iterations: Number of executions dispatch_queue_t queue: queue void (^block) (size_t): block function blocks
Specific examples are as follows:
1. When only one time is executed:
Custom Block function blocks
// Define block void (^block) (void); // encapsulating the execution code in a block BLOCK Myblock = ^() { staticint0; NSLog (@ "count=%d", count++);};
Execute the Dispatch_once function
// Myblock Call // Myblock (); // Myblock (); // will only be executed once, GCD once Static dispatch_once_t predicate;dispatch_once (&predicate, myblock);d ispatch_once (& predicate, myblock);
Run Result: Although executed two times, only one output
--£ ºWu:34.279-gcd-once[3501 :278570] count=0
2, multiple execution:
Customizing a block function
// Define block void (^BLOCK) (size_t); // encapsulates a function into a block BLOCK Myblock = ^(size_t size) { staticint0; NSLog (@ "count=%d", count++); };
Executive Dispatch_apply
// GCD multiple Missions dispatch_apply (50), myblock);
The results of the operation are as follows: 5 executions, 5 outputs
--Ten- - A: $:59.438 to-gcd-apply[3528:280053] Count=0 --Ten- - A: $:59.438 to-gcd-apply[3528:280093] Count=2 --Ten- - A: $:59.438 to-gcd-apply[3528:280094] Count=3 --Ten- - A: $:59.438 to-gcd-apply[3528:280095] Count=1 --Ten- - A: $:59.439 to-gcd-apply[3528:280053] Count=4
IOS:GCD Technology--performs only once and executes multiple dispatch_once and dispatch_apply