IOS:GCD Technology--performs only once and executes multiple dispatch_once and dispatch_apply

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.