DISPATCH_SOURCE_TYPE_TIMER not working

I am creating a timer in a global queue that is configured to fire 45 seconds from the moment it was created, but for some reason it does not work at all. Changing it to shoot, now also does nothing.

The rest of the application is a lot, so maybe something will remove the timer from firing.

Here's how to create a timer:

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 is a good way to try to debug / understand why it is not shooting? If not,

2) Is there a way to list all the tasks that you plan to run in the queue at a certain point in time?

Some work performed by the application cannot be run on the simulator, so I need to debug the test device itself.

+4
source share
2 answers

I had a similar problem. I assume that your timer is a local variable and is released right after you set up. You can make this a property of the class.
Take a look here .

+2
source

Your constants should be unsigned long long, and not unconditional long. Change them:

 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 
+1
source

Source: https://habr.com/ru/post/1336400/


All Articles