我有一个dispatch_source_t
我正在尝试使用的问题。我想用它来延迟 a 的处理PHChange
5 秒,因为 aPHChange
可以在短时间内发生多次。我将不胜感激提供的任何帮助。本质上,我想取消之前的dispatch_source_t
计时器,就像NSTimer
.
@property (nonatomic, strong) dispatch_source_t libraryChangedTimer;
dispatch_source_t CreateTimerDispatchSource(uint64_t interval, uint64_t leeway, dispatch_queue_t queue, dispatch_block_t block)
{
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (timer)
{
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
}
return timer;
}
- (void)libraryChanged:(PHChange *)changeInstance
{
NSLog(@"Called immediately and it shouldn't");
}
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
if (self.libraryChangedTimer)
{
dispatch_source_cancel(self.libraryChangedTimer);
self.libraryChangedTimer = CreateTimerDispatchSource(5ull * NSEC_PER_SEC, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^
{
[self libraryChanged:changeInstance];
dispatch_source_cancel(self.libraryChangedTimer);
});
}
else
{
self.libraryChangedTimer = CreateTimerDispatchSource(5ull * NSEC_PER_SEC, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^
{
[self libraryChanged:changeInstance];
dispatch_source_cancel(self.libraryChangedTimer);
});
}
}