IOS开发之GCD执行延迟操作 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

    1 前言

    使用Dispatch_after ,能够在你想指定一定数量的延迟之后,使用 GCD 来执行代码,

IOS开发之GCD执行延迟操作

。今天我们就来学习一下。

    2 代码实例

    Demo1:

    ZYAppDelegate.m

    [plain]

    - (void) printString:(NSString *)paramString{

    NSLog(@"%@", paramString);

    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

    /*

    推迟三秒执行printString方法

    withObject:传的参数

    */

    [self performSelector:@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 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

    /*

    推迟三秒执行printString方法

    withObject:传的参数

    */

    [self performSelector:@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;

    }

    运行3秒后控制台结果:

    2013-05-10 17:04:52.710 GCDAfterTest[2385:c07] Grand Central Dispatch

    Demo2:

    ZYAppDelegate.m

    [plain]

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

    //设置时间为2

    double delayInSeconds = 2.0;

    //创建一个调度时间,相对于默认时钟或修改现有的调度时间,

电脑资料

IOS开发之GCD执行延迟操作》(https://www.unjs.com)。

    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);

    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 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

    //设置时间为2

    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);

    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;

    }

    运行2秒后控制台结果

最新文章