dispatch_after使用方法详解
来源:互联网 发布:vb wince 全屏 编辑:程序博客网 时间:2024/06/05 02:15
dispatch_after能让我们添加进队列的任务延时执行,该函数并不是在指定时间后执行处理,而只是在指定时间追加处理到dispatch_queue
该方法的第一个参数是time,第二个参数是dispatch_queue,第三个参数是要执行的block。
dispatch_time_t有两种形式的构造方式,第一种相对时间:DISPATCH_TIME_NOW表示现在,NSEC_PER_SEC表示的是秒数,它还提供了NSEC_PER_MSEC表示毫秒。第二种是绝对时间,通过dispatch_walltime函数来获取,dispatch_walltime需要使用一个timespec的结构体来得到dispatch_time_t。
以下代码可以很清楚地看到dispatch_after的执行效果,
dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 10*NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^{
NSLog(@"hello");
});
_count=1;
_timer=[NSTimer timerWithTimeInterval:1 target:self selector:@selector(run) userInfo:nil repeats:YES];
{
if (_count==10) {
[_timer invalidate];
}
_count++;
NSLog(@"the value is %d",_count);
}
result:
2014-12-26 20:30:19.549 testApp[24698:196764] the value is 2
2014-12-26 20:30:20.549 testApp[24698:196764] the value is 3
2014-12-26 20:30:21.549 testApp[24698:196764] the value is 4
2014-12-26 20:30:22.549 testApp[24698:196764] the value is 5
2014-12-26 20:30:23.549 testApp[24698:196764] the value is 6
2014-12-26 20:30:24.549 testApp[24698:196764] the value is 7
2014-12-26 20:30:25.548 testApp[24698:196764] the value is 8
2014-12-26 20:30:26.550 testApp[24698:196764] the value is 9
2014-12-26 20:30:27.550 testApp[24698:196764] the value is 10
2014-12-26 20:30:28.549 testApp[24698:196764] hello
2014-12-26 20:30:28.549 testApp[24698:196764] the value is 11
该方法的第一个参数是time,第二个参数是dispatch_queue,第三个参数是要执行的block。
dispatch_time_t有两种形式的构造方式,第一种相对时间:DISPATCH_TIME_NOW表示现在,NSEC_PER_SEC表示的是秒数,它还提供了NSEC_PER_MSEC表示毫秒。第二种是绝对时间,通过dispatch_walltime函数来获取,dispatch_walltime需要使用一个timespec的结构体来得到dispatch_time_t。
以下代码可以很清楚地看到dispatch_after的执行效果,
dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 10*NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^{
NSLog(@"hello");
});
_count=1;
_timer=[NSTimer timerWithTimeInterval:1 target:self selector:@selector(run) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
{
if (_count==10) {
[_timer invalidate];
}
_count++;
NSLog(@"the value is %d",_count);
}
result:
2014-12-26 20:30:19.549 testApp[24698:196764] the value is 2
2014-12-26 20:30:20.549 testApp[24698:196764] the value is 3
2014-12-26 20:30:21.549 testApp[24698:196764] the value is 4
2014-12-26 20:30:22.549 testApp[24698:196764] the value is 5
2014-12-26 20:30:23.549 testApp[24698:196764] the value is 6
2014-12-26 20:30:24.549 testApp[24698:196764] the value is 7
2014-12-26 20:30:25.548 testApp[24698:196764] the value is 8
2014-12-26 20:30:26.550 testApp[24698:196764] the value is 9
2014-12-26 20:30:27.550 testApp[24698:196764] the value is 10
2014-12-26 20:30:28.549 testApp[24698:196764] hello
2014-12-26 20:30:28.549 testApp[24698:196764] the value is 11
阅读全文
0 0
- dispatch_after使用方法详解
- dispatch_after使用方法详解
- dispatch_after 的使用方法
- dispatch_after-延迟执行的操作使用方法
- 完整详解GCD系列(二)dispatch_after;dispatch_apply;dispatch_once
- 完整详解GCD系列(二)dispatch_after;dispatch_apply;dispatch_once
- 完整详解GCD系列(二)dispatch_after;dispatch_apply;dispatch_
- 完整详解GCD系列(一)dispatch_after;dispatch_apply;dispatch_
- 完整详解swift GCD系列(二)dispatch_after;dispatch_apply;dispatch_once
- iOS学习笔记74-完整详解GCD系列(二)dispatch_after;dispatch_apply;dispatch_once
- [IOS]dispatch_after;dispatch_apply;dispatch_
- GCD(dispatch_after)
- dispatch_after dispatch_group_async dispatch_once
- dispatch_after简单用法
- IOS dispatch_after的取消
- Swift dispatch_after替代ios performSelector
- GCD高级用法(一):dispatch_after
- GCD学习笔记(五):dispatch_after
- Spring3.1.0实现原理分析(九).AOP创建代理对象的过程
- 利用Jsoup实现登录
- Android O版本 行为变更
- 域名解析和cdn 原理
- 阻塞非阻塞与同步异步的区别
- dispatch_after使用方法详解
- maven编译后资源文件发生改变问题
- 开源跨平台开发框架 [记录][U01]
- c/c++ sort函数、qsort函数使用
- Java面试宝典
- 一、数组的封装
- Maven 将本地jar加入maven仓库
- 源码阅读--Glide
- TCP数据流—滑动窗口,拥塞窗口,慢启动,Nagle算法,经受时延的确认等