您的位置:首页 > 其它

GCD 高级用法(一) dispatch after

2015-03-27 13:05 771 查看
- (void)viewDidLoad {

[super viewDidLoad];

//————————————dispatch_after(一定时间后,将执行的操作加入到队列中)————————————

// <#dispatch_time_t when#> 指定时间

//1.第一种用法

/* NSEC_PER_SEC 秒

* NSEC_PER_MSEC 毫秒

* NSEC_PER_USEC 微秒

*/

dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 3ull *NSEC_PER_SEC);

dispatch_after(time, dispatch_get_main_queue(), ^{

//执行操作

NSLog(@"after 3s");

});

//2.第二种用法

//<#dispatch_function_t work#> --执行的c语言方法

dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 3ull *NSEC_PER_SEC), dispatch_get_main_queue(), NULL, fun1);

//3.第三种用法

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5ull * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

NSLog(@"after 5s");

});

#warning mark -注意

//1.不是一定时间后执行相应的任务,而是一定时间后,将执行的操作加入到队列中(队列里面再分配执行的时间)

//2.主线程 RunLoop 1/60秒检测时间,追加的时间范围 3s~(3+1/60)s

}

void fun1()

{

NSLog(@"after 3s");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: