首页 > 其他 > 详细

GCD中如何延迟处理任务

时间:2016-03-15 00:19:14      阅读:186      评论:0      收藏:0      [点我收藏+]

在实际的开发中,经常会遇到想要在指定的时间间隔后执行某个处理

<一>在GCD中提供了dispatch_after函数来完成这一操作

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        <#code to be executed after a specified delay#>

    });

其中(int64_t)(<#delayInSeconds#> * NSEC_PER_SEC) 返回的是时间间隔,数值与 NSEC_PER_SEC的乘积返回毫微秒的数值,

 NSEC_PER_SEC  毫微秒

 NSEC_PER_MSEC 毫秒

 NSEC_PER_USEC 微秒

<注意点>因为Main Dishpatch Queue在主线程的RunLoop中执行,所以比如在每隔1/60秒执行的RunLoop中,Block最快在三秒后执行,最慢在3秒+1/60秒后执行,并且在Main Dispatch Queue有大量追加处理货主线程本身的任务处理有延迟时,这个时间会增加

dispatch_time函数能获得从指定时间开始到第二个参数指定的时间间隔后的时间.

<二>补充,NSObject中提供的线程延迟方法

[self performSelector:@selector(run) withObject:nil afterDelay:2.0];

<三>通过NSTimer来延迟线程执行

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];

GCD中如何延迟处理任务

原文:http://www.cnblogs.com/denz/p/5277621.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!