diff --git a/UIKit+AFNetworking/AFImageDownloader.m b/UIKit+AFNetworking/AFImageDownloader.m index 50e318bc08..74bbab0c02 100644 --- a/UIKit+AFNetworking/AFImageDownloader.m +++ b/UIKit+AFNetworking/AFImageDownloader.m @@ -254,7 +254,7 @@ - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *) completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { dispatch_async(self.responseQueue, ^{ __strong __typeof__(weakSelf) strongSelf = weakSelf; - AFImageDownloaderMergedTask *mergedTask = strongSelf.mergedTasks[URLIdentifier]; + AFImageDownloaderMergedTask *mergedTask = [strongSelf safelyGetMergedTask:URLIdentifier]; if ([mergedTask.identifier isEqual:mergedTaskIdentifier]) { mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier]; if (error) { @@ -404,6 +404,14 @@ - (BOOL)isActiveRequestCountBelowMaximumLimit { return self.activeRequestCount < self.maximumActiveDownloads; } +- (AFImageDownloaderMergedTask *)safelyGetMergedTask:(NSString *)URLIdentifier { + __block AFImageDownloaderMergedTask *mergedTask; + dispatch_sync(self.synchronizationQueue, ^(){ + mergedTask = self.mergedTasks[URLIdentifier]; + }); + return mergedTask; +} + @end #endif