好放弃了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
当表视图“准备好”只是更新。