iPhone iOS如何使用特定的点火时间初始化GCD调度计时器?
我希望在下面代码中的特定时间启动计时器,但找不到一种方法来实例化iPhone iOS如何使用特定的点火时间初始化GCD调度计时器?,iphone,ios,objective-c,timer,grand-central-dispatch,Iphone,Ios,Objective C,Timer,Grand Central Dispatch,我希望在下面代码中的特定时间启动计时器,但找不到一种方法来实例化dispatch\u time\t。我特别不确定如何获得马赫绝对时间来初始化延迟 我看到了一些关于如何在一定延迟后初始化调度时间的答案: dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 这不是我需要的 以下是我想做的: 以毫秒为单位获取当前时间 去掉那部分 再加一秒钟 加20毫秒 使用该时间创建d
dispatch\u time\t
。我特别不确定如何获得马赫绝对时间
来初始化延迟
我看到了一些关于如何在一定延迟后初始化调度时间的答案:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
这不是我需要的
以下是我想做的:
- 以毫秒为单位获取当前时间
- 去掉那部分
- 再加一秒钟
- 加20毫秒
- 使用该时间创建
dispatch\u time\t
- 把它传给计时器
backgroundQueue = dispatch_queue_create("com.eeg.bgqueue", DISPATCH_QUEUE_SERIAL);
synchronizeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue);
//I want to specify when the timer will fire
dispatch_source_set_timer(synchronizeTimer, DISPATCH_TIME_NOW, NSEC_PER_MSEC * updateIntervalms/2.0, NSEC_PER_MSEC * 0);
dispatch_source_set_event_handler(synchronizeTimer, ^
{
// do your processing here
[activeConnection sendNetworkPacketThreadSafe:[circularArray objectAtIndex:lastArrayIndex]];
});
听起来你想要的是一个基于挂钟的计时器,而不是一个基于马赫绝对时间的计时器 请参阅
调度时间(3)
的手册页,该手册页采用struct timespec
基本时间,您可以根据gettimeofday(2)
的结果根据需求配置该时间