IOS开发(63)之GCD执行延迟操作

1 前言

使用Dispatch_after ,能够在你想指定一定数量的延迟之后,使用 GCD 来执行代码。今天我们就来学习一下。

2 代码实例

Demo1:

ZYAppDelegate.m

- (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

- (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秒后控制台结果

2013-05-10 17:06:27.023 GCDAfterTest2[2435:1303] Grand Center Dispatch!

Demo3:

ZYAppDelegate.m

void processSomething(void *paramContext){
    NSLog(@"Processing...");
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //设置时间
    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);
    //调用C函数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;
}

运行2秒后控制台结果

2013-05-10 17:07:27.660 GCDAfterTest3[2476:1303] Processing...

3 结语

以上是所有内容,希望对大家有所帮助。

Demo代码下载:http://download.csdn.net/detail/u010013695/5353603

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值