多執行緒:延時執行
阿新 • • 發佈:2019-02-01
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"start" );
// dispatch_after(dispatch_time_t when,
// dispatch_queue_t queue,
// dispatch_block_t block);
/**
dispatch_time
1. when DISPATCH_TIME_NOW
2. delta (int64_t)(2 * NSEC_PER_SEC)) 納秒
*/
dispatch_time_t after = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));
/**
when 什麼時候執行程式碼
queue 在哪個佇列執行
block 要執行的任務
*/
// 如果是在主執行緒執行延時的程式碼,就用 dispatch_get_main_queue()
// 如果想在子執行緒執行延時的程式碼,就可以用 dispatch_get_global_queue(0, 0)
dispatch_after(after, dispatch_get_global_queue(0, 0), ^{
// 延時執行的程式碼
NSLog(@"hello %@" ,[NSThread currentThread]);
});
NSLog(@"end");
}
@end