iOS中延迟执行的几种方式

- 1.使用NSThread类

[NSThread sleepForTimeInterval:3];

该方法会阻塞当前线程

-2.使用NSObject中的方法

[self performSelector:@selector(download:) withObject:sender afterDelay:timeDelay];

该方法不会阻塞当前线程,在当前线程执行selector

-3.使用gcd

在主线程或由全局队列上的线程异步延迟回调block

dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
   NSLog(@"------task------%@", [NSThread currentThread]);
});
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
     NSLog(@"------task------%@", [NSThread currentThread]);
});
posted @ 2015-06-07 13:45  forflame  阅读(387)  评论(0编辑  收藏  举报