dispatch_source_t
使用しようとしている に問題があります。a は短いスパンで複数回発生PHChange
する可能性があるため、 a の処理を 5 秒間遅らせるために使用したいと考えています。PHChange
提供されたヘルプに感謝します。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);
});
}
}