Merge pull request #2903 from dreampiggy/fix_thread_safe_datatask

Fix the thread safe issue with Downloader and DownloaderOperation during cancel
This commit is contained in:
DreamPiggy 2019-12-06 14:21:02 +08:00 committed by GitHub
commit 421ed8fa8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -9,7 +9,6 @@
#import "SDWebImageDefine.h"
#import "UIImage+Metadata.h"
#import "NSImage+Compatibility.h"
#import "UIImage+ExtendedCacheData.h"
#import "SDAssociatedObject.h"
#pragma mark - Image scale

View File

@ -403,7 +403,12 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
NSOperation<SDWebImageDownloaderOperation> *returnOperation = nil;
for (NSOperation<SDWebImageDownloaderOperation> *operation in self.downloadQueue.operations) {
if ([operation respondsToSelector:@selector(dataTask)]) {
if (operation.dataTask.taskIdentifier == task.taskIdentifier) {
// So we lock the operation here, and in `SDWebImageDownloaderOperation`, we use `@synchonzied (self)`, to ensure the thread safe between these two classes.
NSURLSessionTask *operationTask;
@synchronized (operation) {
operationTask = operation.dataTask;
}
if (operationTask.taskIdentifier == task.taskIdentifier) {
returnOperation = operation;
break;
}