Fix the thread safe issue with Downloader and DownloaderOperation during cancel #2903

This commit is contained in:
DreamPiggy 2019-12-27 14:39:33 +08:00
parent 0cd36c6338
commit 853325139f
1 changed files with 6 additions and 1 deletions

View File

@ -354,7 +354,12 @@
NSOperation<SDWebImageDownloaderOperationInterface> *returnOperation = nil; NSOperation<SDWebImageDownloaderOperationInterface> *returnOperation = nil;
for (NSOperation<SDWebImageDownloaderOperationInterface> *operation in self.downloadQueue.operations) { for (NSOperation<SDWebImageDownloaderOperationInterface> *operation in self.downloadQueue.operations) {
if ([operation respondsToSelector:@selector(dataTask)]) { 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; returnOperation = operation;
break; break;
} }