ShuXiong
ShuXiong

Reputation: 21

How to disable the first fire of the dispatch source timer

I am using Dispatch source timer.

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);

However, I find that the block is called almost immediately after the code above is run over. After that the timer fires every interval. My question is how to disable the first fire?

Upvotes: 2

Views: 643

Answers (2)

DarkDust
DarkDust

Reputation: 92384

The second argument to dispatch_source_set_timer is the time when the timer should fire for the first time. You're setting it to dispatch_walltime(NULL, 0), i.e. "now".

To make it fire for the first time after some interval, pass dispatch_walltime(NULL, interval) instead.

Upvotes: 2

Azul
Azul

Reputation: 91

This could happen when dispatch time is in past or is now. Check your dispatch time.

Upvotes: 0

Related Questions