我正在尝试使用performSelector:withObject:afterDelay:
来快速更新快速更改的表格视图以显示日志消息:
Similar个问题通过先拨打cancelPreviousPerformRequestsWithTarget:
来“重新安排”原始通话。
我也想知道是否:
对于有兴趣的人,我正在重构this。
答案 0 :(得分:1)
好好放弃performSelector
并提出了一个运作良好的GCD解决方案:
创建一个串行队列_consoleQueue = dispatch_queue_create("console_queue", NULL)
。
消息处理代码,其中_updateScheduled
用作忽略后续调用的标志。该标志仅在_consoleQueue
中读取和修改,以确保一致性。
- (void)logMessage:(DDLogMessage *)logMessage
{
dispatch_async(_consoleQueue, ^
{
[_newMessagesBuffer insertObject:logMessage
atIndex:0];
// Ignore subsequent calls when already scheduled
if (_updateScheduled)
return;
NSTimeInterval timeToWaitForNextUpdate = _minIntervalToUpdate + _lastUpdate.timeIntervalSinceNow;
if (timeToWaitForNextUpdate > 0)
{
// Start ignoring calls
_updateScheduled = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeToWaitForNextUpdate * NSEC_PER_SEC)), _consoleQueue, ^
{
[self updateTableViewInConsoleQueue];
// Stop ignoring calls
_updateScheduled = NO;
});
}
else
{
[self updateTableViewInConsoleQueue];
}
});
}
- (void)updateTableViewInConsoleQueue
{
_lastUpdate = NSDate.date;
// ...
}
然而,我仍然不知道如何避免使用_minIntervalToUpdate
,只是在表格视图准备就绪时更新"。