03-dispatch_after

1 dispatch_after 概念

在指定时间之后将任务追加到主队列中。严格来说,这个时间并不是绝对准确的,但想要大致延迟执行任务,dispatch_after函数是很有效的。

    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    // 延时5秒执行任务
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW,(int64_t)(5.0 * NSEC_PER_SEC));
    dispatch_after(time, dispatch_get_main_queue(), ^{
        // 5 秒之后将任务追加到主线程
        NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
        NSLog(@"我来延时了");
    });
    

2 iOS 中 延时执行任务的方式常见的有

  • NSTimer
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
  • performSelector afterDelay

    [self performSelector:@selector(doSomething) withObject:self afterDelay:0.5];
    

猜你喜欢

转载自blog.csdn.net/github_36850997/article/details/85008164