From 3a6d9481c9dd5a0e6a8c586cf6fb7f21127cbe65 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Wed, 7 Aug 2013 21:07:59 -0700 Subject: [PATCH] Call handlers on the main thread synchronously to enhance responsivity (fix #462) --- SDWebImage/MKAnnotationView+WebCache.m | 12 ++--------- SDWebImage/SDImageCache.m | 4 ++-- SDWebImage/SDWebImageCompat.h | 10 +++++++++ SDWebImage/SDWebImageDownloaderOperation.m | 2 +- SDWebImage/SDWebImageManager.m | 2 +- SDWebImage/UIButton+WebCache.m | 24 ++++------------------ SDWebImage/UIImageView+WebCache.m | 24 ++++------------------ 7 files changed, 24 insertions(+), 54 deletions(-) diff --git a/SDWebImage/MKAnnotationView+WebCache.m b/SDWebImage/MKAnnotationView+WebCache.m index 4485fb2c..82dc5b9e 100644 --- a/SDWebImage/MKAnnotationView+WebCache.m +++ b/SDWebImage/MKAnnotationView+WebCache.m @@ -50,7 +50,7 @@ static char operationKey; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong MKAnnotationView *sself = wself; if (!sself) return; @@ -62,15 +62,7 @@ static char operationKey; { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 525e127c..f2411a60 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -280,7 +280,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week [self.memCache setObject:diskImage forKey:key cost:cost]; } - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { doneBlock(diskImage, SDImageCacheTypeDisk); }); @@ -479,7 +479,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week if (completionBlock) { - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { completionBlock(fileCount, totalSize); }); diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index d36c6911..673af06c 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -42,3 +42,13 @@ #endif extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); + +#define dispatch_main_sync_safe(block)\ + if ([NSThread isMainThread])\ + {\ + block();\ + }\ + else\ + {\ + dispatch_sync(dispatch_get_main_queue(), block);\ + } diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index df394ab7..6c765027 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -230,7 +230,7 @@ UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; image = [UIImage decodedImageWithImage:scaledImage]; CGImageRelease(partialImageRef); - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { if (self.completedBlock) { diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index ebe945dc..9c680d96 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -163,7 +163,7 @@ { UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished); }); diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index ba1734dc..3b8420e9 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -49,7 +49,7 @@ static char operationKey; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong UIButton *sself = wself; if (!sself) return; @@ -61,15 +61,7 @@ static char operationKey; { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @@ -112,7 +104,7 @@ static char operationKey; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong UIButton *sself = wself; if (!sself) return; @@ -124,15 +116,7 @@ static char operationKey; { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 747c2398..fa772236 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -56,7 +56,7 @@ static char operationArrayKey; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong UIImageView *sself = wself; if (!sself) return; @@ -69,15 +69,7 @@ static char operationArrayKey; { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @@ -95,7 +87,7 @@ static char operationArrayKey; id operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong UIImageView *sself = wself; [sself stopAnimating]; @@ -112,15 +104,7 @@ static char operationArrayKey; [sself setNeedsLayout]; } [sself startAnimating]; - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; [operationsArray addObject:operation]; }