From 72a4e7b8301e05e01a54434b40255162a34116eb Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Wed, 25 Jun 2014 23:35:08 +0300 Subject: [PATCH] Replace #621Fixed race condition between operation cancelation and loading finish --- SDWebImage/SDWebImageDownloaderOperation.m | 32 +++++++++------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 474dbdf..1646ebe 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -335,48 +335,42 @@ } - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection { - CFRunLoopStop(CFRunLoopGetCurrent()); - self.connection = nil; - - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; - SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock; + @synchronized(self) { + CFRunLoopStop(CFRunLoopGetCurrent()); + self.thread = nil; + self.connection = nil; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; + } if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) { responseFromCached = NO; } - if (completionBlock) { + if (completionBlock) + { if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) { completionBlock(nil, nil, nil, YES); - self.completionBlock = nil; - [self done]; } else { - UIImage *image = [UIImage sd_imageWithData:self.imageData]; - NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; image = [self scaledImageForKey:key image:image]; - - if (!image.images) // Do not force decod animated GIFs - { + + // Do not force decoding animated GIFs + if (!image.images) { image = [UIImage decodedImageWithImage:image]; } - if (CGSizeEqualToSize(image.size, CGSizeZero)) { completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES); } else { completionBlock(image, self.imageData, nil, YES); } - self.completionBlock = nil; - [self done]; } } - else { - [self done]; - } + self.completionBlock = nil; + [self done]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {