我想在 GCD 块中创建一个计时器(它将每 2 秒触发一次并调用一个方法)以将其用作后台任务。但正如我所见,计时器永远不会触发。这是我的代码:
- (void)startMessaging
{
BOOL queue = YES;
dispatch_queue_t _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, _queue);
dispatch_source_set_timer(timerSource, dispatch_walltime(NULL, 0), 2ull * NSEC_PER_SEC,1ull * NSEC_PER_SEC );
dispatch_source_set_event_handler(timerSource, ^{
if (queue) {
[self observeNewMsgs];
}
});
dispatch_resume(timerSource);
}
- (void)observeNewMsgs
{
NSLog(@"JUST TO TEST");
// Staff code...
}
那么这有什么问题呢?我怎样才能解决这个问题?