GCD 高级用法(一) dispatch after

来源:互联网 发布:用手机网络给电脑上网 编辑:程序博客网 时间:2024/06/14 10:52
- (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");

}
0 0
原创粉丝点击