From 0643cb81c37f54384325fe6caff3ac118affff42 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 23 Jul 2022 21:08:55 +0800 Subject: [PATCH] Fix the rare case when cancel an async disk cache query may cause twice callback One is sync and another is async --- SDWebImage/Core/SDImageCache.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/SDWebImage/Core/SDImageCache.m b/SDWebImage/Core/SDImageCache.m index 1dc25feb..cce92f64 100644 --- a/SDWebImage/Core/SDImageCache.m +++ b/SDWebImage/Core/SDImageCache.m @@ -662,6 +662,13 @@ static NSString * _defaultDiskCacheDirectory; } if (doneBlock) { dispatch_async(dispatch_get_main_queue(), ^{ + // Dispatch from IO queue to main queue need time, user may call cancel during the dispatch timing + // This check is here to avoid double callback (one is from `SDImageCacheToken` in sync) + @synchronized (operation) { + if (operation.isCancelled) { + return; + } + } doneBlock(diskImage, diskData, SDImageCacheTypeDisk); }); }