Jonas Anderson
Jonas Anderson

Reputation: 1987

DISPATCH_SOURCE_TYPE_TIMER not firing

I'm creating a timer on the global queue, configured to fire 45 seconds from creation time but for some reason, it doesn't seem to fire at all. Changing it to fire now also doesn't do anything.

The rest app has a lot going on so there's probably something pre-empting the timer from firing.

This is how the timer is created:

dispatch_queue_t globalQueue = 
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); 

timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue); 
if (timer) {

// start 45 seconds for now
dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, 45ull * NSEC_PER_SEC);
uint64_t interval = 15ull * NSEC_PER_SEC; // every 15 seconds, converted to nanosecs

// leeway:8 microseconds
dispatch_source_set_timer(timer, startTime, interval, 8000ull); 

dispatch_source_set_event_handler(timer, block); // block is passed in

dispatch_resume(timer);

1) What's a good way to try to debug/figure out why it's not firing? If not,

2) Is there a way to list all given tasks that are scheduled to run on a queue at a specific point in time?

Some of the work done by the app can't be launched on the simulator so I need to debug over on the test device itself.

Upvotes: 4

Views: 3582

Answers (2)

Sebastian Cichosz
Sebastian Cichosz

Reputation: 909

I had similar problem. My guess is that your timer is local variable and is released just after you set things up. You could make it a class property.
Have a look here.

Upvotes: 2

Jason Coco
Jason Coco

Reputation: 78393

Your constants need to be unsigned long longs, not unsigned longs. Change to these:

dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, 45ull * NSEC_PER_SEC);
uint64_t interval = 15ull * NSEC_PER_SEC; // every 15 seconds, converted to nanosecs

Upvotes: 1

Related Questions