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