@@ -223,7 +223,6 @@ - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
223
223
return nil ;
224
224
}
225
225
226
- SD_LOCK (_operationsLock);
227
226
id downloadOperationCancelToken;
228
227
// When different thumbnail size download with same url, we need to make sure each callback called with desired size
229
228
id <SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
@@ -234,9 +233,17 @@ - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
234
233
cacheKey = url.absoluteString ;
235
234
}
236
235
SDImageCoderOptions *decodeOptions = SDGetDecodeOptionsFromContext (context, [self .class imageOptionsFromDownloaderOptions: options], cacheKey);
236
+ SD_LOCK (_operationsLock);
237
237
NSOperation <SDWebImageDownloaderOperation> *operation = [self .URLOperations objectForKey: url];
238
238
// There is a case that the operation may be marked as finished or cancelled, but not been removed from `self.URLOperations`.
239
- BOOL shouldNotReuseOperation = !operation || operation.isFinished || operation.isCancelled || SDWebImageDownloaderOperationGetCompleted (operation);
239
+ BOOL shouldNotReuseOperation;
240
+ if (operation) {
241
+ @synchronized (operation) {
242
+ shouldNotReuseOperation = operation.isFinished || operation.isCancelled || SDWebImageDownloaderOperationGetCompleted (operation);
243
+ }
244
+ } else {
245
+ shouldNotReuseOperation = YES ;
246
+ }
240
247
if (shouldNotReuseOperation) {
241
248
operation = [self createDownloaderOperationWithUrl: url options: options context: context];
242
249
if (!operation) {
@@ -269,15 +276,6 @@ - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
269
276
@synchronized (operation) {
270
277
downloadOperationCancelToken = [operation addHandlersForProgress: progressBlock completed: completedBlock decodeOptions: decodeOptions];
271
278
}
272
- if (!operation.isExecuting ) {
273
- if (options & SDWebImageDownloaderHighPriority) {
274
- operation.queuePriority = NSOperationQueuePriorityHigh;
275
- } else if (options & SDWebImageDownloaderLowPriority) {
276
- operation.queuePriority = NSOperationQueuePriorityLow;
277
- } else {
278
- operation.queuePriority = NSOperationQueuePriorityNormal;
279
- }
280
- }
281
279
}
282
280
SD_UNLOCK (_operationsLock);
283
281
@@ -518,8 +516,10 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
518
516
// Identify the operation that runs this task and pass it the delegate method
519
517
NSOperation <SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask: task];
520
518
if (dataOperation) {
521
- // Mark the downloader operation `isCompleted = YES`, no longer re-use this operation when new request comes in
522
- SDWebImageDownloaderOperationSetCompleted (dataOperation, true );
519
+ @synchronized (dataOperation) {
520
+ // Mark the downloader operation `isCompleted = YES`, no longer re-use this operation when new request comes in
521
+ SDWebImageDownloaderOperationSetCompleted (dataOperation, true );
522
+ }
523
523
}
524
524
if ([dataOperation respondsToSelector: @selector (URLSession:task:didCompleteWithError: )]) {
525
525
[dataOperation URLSession: session task: task didCompleteWithError: error];
0 commit comments