From 8f34f98c61d3ab250a9a12d507e45b299de8c022 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 11 Apr 2021 11:30:51 +0800 Subject: [PATCH 1/2] Change the willTerminate auto clean cache logic into sync version --- SDWebImage/Core/SDImageCache.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SDWebImage/Core/SDImageCache.m b/SDWebImage/Core/SDImageCache.m index e9f1d584..0744eefd 100644 --- a/SDWebImage/Core/SDImageCache.m +++ b/SDWebImage/Core/SDImageCache.m @@ -672,7 +672,11 @@ static NSString * _defaultDiskCacheDirectory; #if SD_UIKIT || SD_MAC - (void)applicationWillTerminate:(NSNotification *)notification { - [self deleteOldFilesWithCompletionBlock:nil]; + // On iOS/macOS, the async opeartion to remove exipred data will be terminated quickly + // Try using the sync operation to ensure we reomve the exipred data + dispatch_sync(self.ioQueue, ^{ + [self.diskCache removeExpiredData]; + }); } #endif From 51176c7b74646cac983b2c83454ee431827dc193 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 11 Apr 2021 12:53:54 +0800 Subject: [PATCH 2/2] Added new disk cache config `shouldRemoveExpiredDataWhenTerminate` --- SDWebImage/Core/SDImageCache.m | 3 +++ SDWebImage/Core/SDImageCacheConfig.h | 6 ++++++ SDWebImage/Core/SDImageCacheConfig.m | 2 ++ 3 files changed, 11 insertions(+) diff --git a/SDWebImage/Core/SDImageCache.m b/SDWebImage/Core/SDImageCache.m index 0744eefd..c1f99a89 100644 --- a/SDWebImage/Core/SDImageCache.m +++ b/SDWebImage/Core/SDImageCache.m @@ -674,6 +674,9 @@ static NSString * _defaultDiskCacheDirectory; - (void)applicationWillTerminate:(NSNotification *)notification { // On iOS/macOS, the async opeartion to remove exipred data will be terminated quickly // Try using the sync operation to ensure we reomve the exipred data + if (!self.config.shouldRemoveExpiredDataWhenTerminate) { + return; + } dispatch_sync(self.ioQueue, ^{ [self.diskCache removeExpiredData]; }); diff --git a/SDWebImage/Core/SDImageCacheConfig.h b/SDWebImage/Core/SDImageCacheConfig.h index 4d6e79b0..f2d19203 100644 --- a/SDWebImage/Core/SDImageCacheConfig.h +++ b/SDWebImage/Core/SDImageCacheConfig.h @@ -67,6 +67,12 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) { */ @property (assign, nonatomic) BOOL shouldRemoveExpiredDataWhenEnterBackground; +/** + * Whether or not to remove the expired disk data when application been terminated. This operation is processed in sync to ensure clean up. + * Defaults to YES. + */ +@property (assign, nonatomic) BOOL shouldRemoveExpiredDataWhenTerminate; + /** * The reading options while reading cache from disk. * Defaults to 0. You can set this to `NSDataReadingMappedIfSafe` to improve performance. diff --git a/SDWebImage/Core/SDImageCacheConfig.m b/SDWebImage/Core/SDImageCacheConfig.m index 40a53348..ad5bcaad 100644 --- a/SDWebImage/Core/SDImageCacheConfig.m +++ b/SDWebImage/Core/SDImageCacheConfig.m @@ -29,6 +29,7 @@ static const NSInteger kDefaultCacheMaxDiskAge = 60 * 60 * 24 * 7; // 1 week _shouldCacheImagesInMemory = YES; _shouldUseWeakMemoryCache = YES; _shouldRemoveExpiredDataWhenEnterBackground = YES; + _shouldRemoveExpiredDataWhenTerminate = YES; _diskCacheReadingOptions = 0; _diskCacheWritingOptions = NSDataWritingAtomic; _maxDiskAge = kDefaultCacheMaxDiskAge; @@ -46,6 +47,7 @@ static const NSInteger kDefaultCacheMaxDiskAge = 60 * 60 * 24 * 7; // 1 week config.shouldCacheImagesInMemory = self.shouldCacheImagesInMemory; config.shouldUseWeakMemoryCache = self.shouldUseWeakMemoryCache; config.shouldRemoveExpiredDataWhenEnterBackground = self.shouldRemoveExpiredDataWhenEnterBackground; + config.shouldRemoveExpiredDataWhenTerminate = self.shouldRemoveExpiredDataWhenTerminate; config.diskCacheReadingOptions = self.diskCacheReadingOptions; config.diskCacheWritingOptions = self.diskCacheWritingOptions; config.maxDiskAge = self.maxDiskAge;