我正在全局队列上创建一个计时器,配置为从创建时间开始45秒,但由于某种原因,它似乎根本不会触发。现在将它改为火也没有做任何事情。
剩下的应用程序有很多事情发生,所以可能会有一些事情要先解决计时器的问题。
这是计时器的创建方式:
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)尝试调试/弄清楚它为什么不被解雇的好方法是什么?如果没有,
2)有没有办法列出计划在特定时间点在队列上运行的所有给定任务?
应用程序完成的部分工作无法在模拟器上启动,因此我需要在测试设备上进行调试。
答案 0 :(得分:2)
我有类似的问题。我的猜测是你的计时器是局部变量,并在你设置后立即释放。你可以把它变成一个阶级财产 看看here。
答案 1 :(得分:1)
你的常量需要是unsigned long longs,而不是unsigned longs。改为:
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