バックグラウンド タスクとして使用するために、GCD ブロック (2 秒ごとに 1 回起動し、メソッドを呼び出す) でタイマーを作成したいと考えています。しかし、私が見るように、タイマーは決して起動しません。これが私のコードです:
- (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...
}
では、これの何が問題なのですか?どうすればこれを修正できますか?