From 6b8075a2e1849ffff5bde0496f3cd841e16364ec Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 19 Sep 2022 17:33:04 +0800 Subject: [PATCH] Using the same synchornized to guard _cancelled status, which need recursive lock DO NOT USE SD_LOCK (os_unfair_lock), which not support recursive --- SDWebImage/Core/SDWebImageManager.h | 3 +++ SDWebImage/Core/SDWebImageManager.m | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/SDWebImage/Core/SDWebImageManager.h b/SDWebImage/Core/SDWebImageManager.h index ee90724e..fcac52ea 100644 --- a/SDWebImage/Core/SDWebImageManager.h +++ b/SDWebImage/Core/SDWebImageManager.h @@ -29,6 +29,9 @@ typedef void(^SDInternalCompletionBlock)(UIImage * _Nullable image, NSData * _Nu */ - (void)cancel; +/// Whether the operation has been cancelled. +@property (nonatomic, assign, readonly, getter=isCancelled) BOOL cancelled; + /** The cache operation from the image cache query */ diff --git a/SDWebImage/Core/SDWebImageManager.m b/SDWebImage/Core/SDWebImageManager.m index 3b29632d..99b83e55 100644 --- a/SDWebImage/Core/SDWebImageManager.m +++ b/SDWebImage/Core/SDWebImageManager.m @@ -803,12 +803,20 @@ static id _defaultImageLoader; @implementation SDWebImageCombinedOperation +- (BOOL)isCancelled { + // Need recursive lock (user's cancel block may check isCancelled), do not use SD_LOCK + @synchronized (self) { + return _cancelled; + } +} + - (void)cancel { + // Need recursive lock (user's cancel block may check isCancelled), do not use SD_LOCK @synchronized(self) { - if (self.isCancelled) { + if (_cancelled) { return; } - self.cancelled = YES; + _cancelled = YES; if (self.cacheOperation) { [self.cacheOperation cancel]; self.cacheOperation = nil;