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
[Plain]
-(Void) printString :( NSString *) paramString {
NSLog (@ "% @", paramString );
}
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
/*
Three seconds later to 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;
}
-(Void) printString :( NSString *) paramString {
NSLog (@ "% @", paramString );
}
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
/*
Three seconds later to 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
[Plain]
-(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 modify the existing scheduling time.
Dispatch_time_t delayInNanoSeconds = dispatch_time (DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC );
// Two nanoseconds of execution delayed
Dispatch_queue_t concurrentQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_after (delayInNanoSeconds, concurrentQueue, ^ (void ){
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;
}
-(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 modify the existing scheduling time.
Dispatch_time_t delayInNanoSeconds = dispatch_time (DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC );
// Two nanoseconds of execution delayed
Dispatch_queue_t concurrentQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_after (delayInNanoSeconds, concurrentQueue, ^ (void ){
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
[Plain]
Void processSomething (void * paramContext ){
NSLog (@ "Processing ...");
}
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( 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 = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
// Call the C function processSomething
Dispatch_after_f (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;
}
Void processSomething (void * paramContext ){
NSLog (@ "Processing ...");
}
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( 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 = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
// Call the C function processSomething
Dispatch_after_f (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...