GCD定时器

turboksiOS194

NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时。

而GCD的定时器会更加准时。

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_queue_create("001", DISPATCH_QUEUE_CONCURRENT));

    dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 1.0 * NSEC_PER_SEC);

    dispatch_source_set_event_handler(_timer, ^{

        [self test];

    });

    dispatch_resume(_timer);

}

- (void)viewWillDisappear:(BOOL)animated{

    _timer = nil;

}

- (void)test{

    NSLog(@"%s",__func__);

}

- (void)dealloc{

    NSLog(@"%s",__func__);

}


相关文章

OC的实例对象、类对象、元类对象的关系

OC的实例对象、类对象、元类对象的关系

Objective-C中的对象、简称OC对象,主要分为三种:instance对象(实例对象)、class对象(类对象)、meta-class对象(元类对象)。一:instance对象instance对...

多线程举例

多线程举例

举例一:- (void)viewDidLoad {    [super viewDidLoad];    dispatch_queue_t queue = di...

iOS图片圆角设置

iOS图片圆角设置

    UIImageView * vv = [[UIImageView alloc] initWithFrame:CGRectMake(50, 500, 250, 25...

WKWebView

WKWebView

<!DOCTYPE html><head>    <title>HTML</title>    <meta...

iOS 仿抖音打卡美好中国

iOS 仿抖音打卡美好中国

效果图这几天打卡中国挺火的,刚好有点时间就想着看看能不能模仿一下。Scrollview + CADisplayLink 设置背景UILongPressGestureRecognizer长按手...

iOS一行代码搞定自定义AlertView

iOS一行代码搞定自定义AlertView

效果图走一走:使用方式: pod  'KSAlertView'举例使用,单按钮Alert: [[[KSAlertView alloc] initWithT...