Skip to content

Commit

Permalink
Merge pull request #783 from bpoplauschi/race_condition
Browse files Browse the repository at this point in the history
Replace #621Fixed race condition between operation cancelation and loading finish
  • Loading branch information
bpoplauschi committed Jul 11, 2014
2 parents 894a1b1 + 72a4e7b commit 39db378
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions SDWebImage/SDWebImageDownloaderOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -335,48 +335,42 @@ - (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image {
}

- (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 {
Expand Down

0 comments on commit 39db378

Please sign in to comment.