Я столкнулся с каким-то странным поведением Grand Central Dispatch timer. Он прерывает свой период стрельбы и замерзает в течение многих секунд. Хотя мне нужно пинговать мой сервер, чтобы оставаться «онлайн», это поведение сильно непригодно.Поведение Strange GCD-Timer
Здесь код создания таймера.
// pingTimer and pingQueue are class members
- (void)createPingTimerSource
{
// check timer exists
if(pingTimer)
{
// suspend source and cancel
[self setPingTimerSuspended:YES];
dispatch_source_cancel(pingTimer);
}
// check having queue, create if doesn't exist
if(!pingQueue)
pingQueue = dispatch_queue_create(kDispatchTimerQueueLabel, NULL);
// create timer dispatch source
pingTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, pingQueue);
dispatch_source_set_timer(pingTimer, dispatch_time(DISPATCH_TIME_NOW, 5*NSEC_PER_MSEC), 5*NSEC_PER_MSEC, NSEC_PER_SEC/10);
// set event handler
dispatch_source_set_event_handler(pingTimer,^{
printf("[%llu] gcd timer fired.\n", mach_absolute_time()/NSEC_PER_SEC);
dispatch_async(dispatch_get_main_queue(), ^{
[self sendPingToServer];
});
});
// set cancel handler
dispatch_source_set_cancel_handler(pingTimer, ^{
// release dispatch source if exists
if(pingTimer)
dispatch_release(pingTimer);
// check timer queue exists and release if does
if(pingQueue)
dispatch_release(pingQueue);
});
}
Здесь бревенчатая консоль снята.
Спасибо за помощь.
Да, вы правы. Спасибо за помощь! – Astoria