From 331053d26e47969176b3c14c794169154e4766f4 Mon Sep 17 00:00:00 2001 From: Matej Bukovinski Date: Mon, 19 Nov 2012 12:01:14 +0100 Subject: [PATCH] Calling completion callbacks in the main thread. - made sure this is true even if the downloader is used on its own - SDWebImageManager doesn't need to perform a dispatch to the main thread any longer --- SDWebImage/SDWebImageDownloaderOperation.m | 8 +++++- SDWebImage/SDWebImageManager.m | 29 ++++++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index f317a0f9..ac0821b9 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -237,7 +237,13 @@ { UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, [UIImage imageWithCGImage:partialImageRef])]; CGImageRelease(partialImageRef); - if (self.completedBlock) self.completedBlock(image, nil, nil, NO); + dispatch_async(dispatch_get_main_queue(), ^ + { + if (self.completedBlock) + { + self.completedBlock(image, nil, nil, NO); + } + }); } } diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 380ea190..29c6f00e 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -99,24 +99,21 @@ if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload; __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { - dispatch_async(dispatch_get_main_queue(), ^ + completedBlock(downloadedImage, error, SDImageCacheTypeNone, finished); + + if (error) { - completedBlock(downloadedImage, error, SDImageCacheTypeNone, finished); + [self.failedURLs addObject:url]; + } + else if (downloadedImage && finished) + { + [self.imageCache storeImage:downloadedImage imageData:data forKey:key toDisk:YES]; + } - if (error) - { - [self.failedURLs addObject:url]; - } - else if (downloadedImage && finished) - { - [self.imageCache storeImage:downloadedImage imageData:data forKey:key toDisk:YES]; - } - - if (finished) - { - [self.runningOperations removeObject:operation]; - } - }); + if (finished) + { + [self.runningOperations removeObject:operation]; + } }]; operation.cancelBlock = ^{[subOperation cancel];}; }