Просмотр исходного кода

Merge pull request #783 from bpoplauschi/race_condition

Replace #621Fixed race condition between operation cancelation and loading finish
Bogdan Poplauschi 11 лет назад
Родитель
Сommit
39db378082
1 измененных файлов с 13 добавлено и 19 удалено
  1. 13 19
      SDWebImage/SDWebImageDownloaderOperation.m

+ 13 - 19
SDWebImage/SDWebImageDownloaderOperation.m

@@ -335,48 +335,42 @@
 }
 }
 
 
 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
-    CFRunLoopStop(CFRunLoopGetCurrent());
-    self.connection = nil;
-
-    [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
-
     SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
     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]) {
     if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
         responseFromCached = NO;
         responseFromCached = NO;
     }
     }
     
     
-    if (completionBlock) {
+    if (completionBlock)
+    {
         if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) {
         if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) {
             completionBlock(nil, nil, nil, YES);
             completionBlock(nil, nil, nil, YES);
-            self.completionBlock = nil;
-            [self done];
         }
         }
         else {
         else {
-
             UIImage *image = [UIImage sd_imageWithData:self.imageData];
             UIImage *image = [UIImage sd_imageWithData:self.imageData];
-
             NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
             NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
             image = [self scaledImageForKey:key image:image];
             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];
                 image = [UIImage decodedImageWithImage:image];
             }
             }
-
             if (CGSizeEqualToSize(image.size, CGSizeZero)) {
             if (CGSizeEqualToSize(image.size, CGSizeZero)) {
                 completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES);
                 completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES);
             }
             }
             else {
             else {
                 completionBlock(image, self.imageData, nil, YES);
                 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 {
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {