@interface ViewController ()
@property(nonatomic,strong)dispatch_source_t timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建队列(全局并发队列)
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//创建定时器
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//延迟5s启动
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (ino64_t)(5.0*NSEC_PER_SEC));
//设置定时器的各种属性(开始时间,相隔时间)
dispatch_source_set_timer(self.timer, start, 2 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//设置回调
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"---");
});
//开启定时器
dispatch_resume(self.timer);
NSLog(@"******");
}
log: