GCD for iOS development (63) performs delayed operations

Source: Internet
Author: User
1 Preface

 

With dispatch_after, you can use GCD to execute code after you want to specify a certain number of latencies. Let's take a look at it today.

 

2. code example

Demo1:

Zyappdelegate. m

 

-(Void) printstring :( nsstring *) paramstring {nslog (@ "% @", paramstring);}-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {/* three seconds later. Execute the printstring method withobject: The passed parameter */[self defined mselector: @ selector (printstring :) withobject: @ "Grand Central dispatch" afterdelay: 3.0]; self. window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease]; // override point for customization after application launch. self. viewcontroller = [[[zyviewcontroller alloc] initwithnibname: @ "zyviewcontroller" Bundle: Nil] autorelease]; self. window. rootviewcontroller = self. viewcontroller; [self. window makekeyandvisible]; return yes ;}

 

Console result after 3 seconds of running:

 

17:04:52. 710 gcdaftertest [2385: c07] Grand Central Dispatch

Demo2:

Zyappdelegate. m

 

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {// set the time to 2 double delayinseconds = 2.0; // create a scheduling time, relative to the default clock or the current scheduling time. Interval seconds = dispatch_time (seconds, delayinseconds * nsec_per_sec); // delay two nanoseconds of execution; interval concurrentqueue = second (milliseconds, 0); dispatch_after (delayinnanoseconds, concurrentqueue, ^) {nslog (@ "grand center dispatch! ") ;}); Self. window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease]; // override point for customization after application launch. self. viewcontroller = [[[zyviewcontroller alloc] initwithnibname: @ "zyviewcontroller" Bundle: Nil] autorelease]; self. window. rootviewcontroller = self. viewcontroller; [self. window makekeyandvisible]; return yes ;}

 

Console result after 2 seconds of Running

 

17:06:27. 023 gcdaftertest2 [2435: 1303] Grand center dispatch!

Demo3:

Zyappdelegate. m

 

Void processsomething (void * paramcontext) {nslog (@ "processing... ");}-(bool) Application :( uiapplication *) application example :( nsdictionary *) launchoptions {// set the time double delayinseconds = 2.0; dispatch_time_t delayinnanoseconds = dispatch_time (dispatch_time_now, delayinseconds * nsec_per_sec); dispatch_queue_t concurrentqueue = Queue (queue, 0); // call the C function processsomething Queue (delayinnanoseconds, concurrentqueue, null, processsomething) self. window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease]; // override point for customization after application launch. self. viewcontroller = [[[zyviewcontroller alloc] initwithnibname: @ "zyviewcontroller" Bundle: Nil] autorelease]; self. window. rootviewcontroller = self. viewcontroller; [self. window makekeyandvisible]; return yes ;}

 

Console result after 2 seconds of Running

 

17:07:27. 660 gcdaftertest3 [2476: 1303] processing...

3 conclusion

The above is all content and I hope it will help you.

DEMO code download: http://download.csdn.net/detail/u010013695/5353603

 

Related Article

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.