Merge pull request #3393 from alexander-gaidukov/synchronize_operation_cancellation

Synchronise getter and setter of the cancelled property of the SDWebImageCombinedOperation
This commit is contained in:
DreamPiggy 2022-09-09 18:15:44 +08:00 committed by GitHub
commit 3a6e01ab20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 16 deletions

View File

@ -17,7 +17,9 @@
static id<SDImageCache> _defaultImageCache; static id<SDImageCache> _defaultImageCache;
static id<SDImageLoader> _defaultImageLoader; static id<SDImageLoader> _defaultImageLoader;
@interface SDWebImageCombinedOperation () @interface SDWebImageCombinedOperation () {
SD_LOCK_DECLARE(_cancelledLock); // a lock to keep the access to `cancelled` thread-safe
}
@property (assign, nonatomic, getter = isCancelled) BOOL cancelled; @property (assign, nonatomic, getter = isCancelled) BOOL cancelled;
@property (strong, nonatomic, readwrite, nullable) id<SDWebImageOperation> loaderOperation; @property (strong, nonatomic, readwrite, nullable) id<SDWebImageOperation> loaderOperation;
@ -803,22 +805,38 @@ static id<SDImageLoader> _defaultImageLoader;
@implementation SDWebImageCombinedOperation @implementation SDWebImageCombinedOperation
- (void)cancel { - (instancetype)init {
@synchronized(self) { if (self = [super init]) {
if (self.isCancelled) { SD_LOCK_INIT(_cancelledLock);
return;
}
self.cancelled = YES;
if (self.cacheOperation) {
[self.cacheOperation cancel];
self.cacheOperation = nil;
}
if (self.loaderOperation) {
[self.loaderOperation cancel];
self.loaderOperation = nil;
}
[self.manager safelyRemoveOperationFromRunning:self];
} }
return self;
}
- (BOOL)isCancelled {
BOOL isCancelled = NO;
SD_LOCK(_cancelledLock);
isCancelled = _cancelled;
SD_UNLOCK(_cancelledLock);
return isCancelled;
}
- (void)cancel {
SD_LOCK(_cancelledLock);
if (_cancelled) {
return;
}
_cancelled = YES;
if (self.cacheOperation) {
[self.cacheOperation cancel];
self.cacheOperation = nil;
}
if (self.loaderOperation) {
[self.loaderOperation cancel];
self.loaderOperation = nil;
}
[self.manager safelyRemoveOperationFromRunning:self];
SD_UNLOCK(_cancelledLock);
} }
@end @end