Authored by Bogdan Poplauschi

Merge pull request #783 from bpoplauschi/race_condition

Replace #621Fixed race condition between operation cancelation and loading finish
@@ -335,48 +335,42 @@ @@ -335,48 +335,42 @@
335 } 335 }
336 336
337 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection { 337 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
338 - CFRunLoopStop(CFRunLoopGetCurrent());  
339 - self.connection = nil;  
340 -  
341 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];  
342 -  
343 SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock; 338 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 + }
344 345
345 if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) { 346 if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
346 responseFromCached = NO; 347 responseFromCached = NO;
347 } 348 }
348 349
349 - if (completionBlock) { 350 + if (completionBlock)
  351 + {
350 if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) { 352 if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) {
351 completionBlock(nil, nil, nil, YES); 353 completionBlock(nil, nil, nil, YES);
352 - self.completionBlock = nil;  
353 - [self done];  
354 } 354 }
355 else { 355 else {
356 -  
357 UIImage *image = [UIImage sd_imageWithData:self.imageData]; 356 UIImage *image = [UIImage sd_imageWithData:self.imageData];
358 -  
359 NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; 357 NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
360 image = [self scaledImageForKey:key image:image]; 358 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) {
364 image = [UIImage decodedImageWithImage:image]; 362 image = [UIImage decodedImageWithImage:image];
365 } 363 }
366 -  
367 if (CGSizeEqualToSize(image.size, CGSizeZero)) { 364 if (CGSizeEqualToSize(image.size, CGSizeZero)) {
368 completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES); 365 completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES);
369 } 366 }
370 else { 367 else {
371 completionBlock(image, self.imageData, nil, YES); 368 completionBlock(image, self.imageData, nil, YES);
372 } 369 }
373 - self.completionBlock = nil;  
374 - [self done];  
375 } 370 }
376 } 371 }
377 - else {  
378 - [self done];  
379 - } 372 + self.completionBlock = nil;
  373 + [self done];
380 } 374 }
381 375
382 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 376 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {