GCD:如何更改计时器火警间隔

时间:2012-06-17 08:43:52

标签: iphone ios objective-c ipad grand-central-dispatch

无论如何,这听起来可能是一个新手问题,我对GCD很新。

我已经关注了代码:

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);
}

其中someMethod是:

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

如何更改someMethod中的timer火力间隔属性?

1 个答案:

答案 0 :(得分:9)

我自己得到答案,用新的间隔值调用dispatch_source_set_timer就足够了