GCD: How to change timer fire interval

你。 提交于 2019-12-21 04:43:15

问题


This may sound a newbie question anyway, I'm very new to GCD

I've following code :

int interval = 2;
int leeway = 0;

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (timer) {
    dispatch_source_set_timer(timer, dispatch_walltime(DISPATCH_TIME_NOW, NSEC_PER_SEC * interval), interval * NSEC_PER_SEC, leeway);
    dispatch_source_set_event_handler(timer, ^{
        [self someMethod];
    });
    dispatch_resume(timer);
}

Where someMethod is :

- (void)someMethod
{
    NSLog(@"Thread 1");
}

How do I change the timer's fire interval property in someMethod ?


回答1:


Got the answer on my own, calling dispatch_source_set_timer with new interval value is enough



来源:https://stackoverflow.com/questions/11070026/gcd-how-to-change-timer-fire-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!