Skip to content

Commit 39db378

Browse files
committed
Merge pull request #783 from bpoplauschi/race_condition
Replace #621Fixed race condition between operation cancelation and loading finish
2 parents 894a1b1 + 72a4e7b commit 39db378

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

SDWebImage/SDWebImageDownloaderOperation.m

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -335,48 +335,42 @@ - (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image {
335335
}
336336

337337
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
338-
CFRunLoopStop(CFRunLoopGetCurrent());
339-
self.connection = nil;
340-
341-
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
342-
343338
SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
339+
@synchronized(self) {
340+
CFRunLoopStop(CFRunLoopGetCurrent());
341+
self.thread = nil;
342+
self.connection = nil;
343+
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
344+
}
344345

345346
if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
346347
responseFromCached = NO;
347348
}
348349

349-
if (completionBlock) {
350+
if (completionBlock)
351+
{
350352
if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) {
351353
completionBlock(nil, nil, nil, YES);
352-
self.completionBlock = nil;
353-
[self done];
354354
}
355355
else {
356-
357356
UIImage *image = [UIImage sd_imageWithData:self.imageData];
358-
359357
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
360358
image = [self scaledImageForKey:key image:image];
361-
362-
if (!image.images) // Do not force decod animated GIFs
363-
{
359+
360+
// Do not force decoding animated GIFs
361+
if (!image.images) {
364362
image = [UIImage decodedImageWithImage:image];
365363
}
366-
367364
if (CGSizeEqualToSize(image.size, CGSizeZero)) {
368365
completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES);
369366
}
370367
else {
371368
completionBlock(image, self.imageData, nil, YES);
372369
}
373-
self.completionBlock = nil;
374-
[self done];
375370
}
376371
}
377-
else {
378-
[self done];
379-
}
372+
self.completionBlock = nil;
373+
[self done];
380374
}
381375

382376
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

0 commit comments

Comments
 (0)