Call handlers on the main thread synchronously to enhance responsivity (fix #462)

This commit is contained in:
Olivier Poitrey 2013-08-07 21:07:59 -07:00
parent f07a34da59
commit 3a6d9481c9
7 changed files with 24 additions and 54 deletions

View File

@ -50,7 +50,7 @@ static char operationKey;
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{ {
if (!wself) return; if (!wself) return;
void (^block)(void) = ^ dispatch_main_sync_safe(^
{ {
__strong MKAnnotationView *sself = wself; __strong MKAnnotationView *sself = wself;
if (!sself) return; if (!sself) return;
@ -62,15 +62,7 @@ static char operationKey;
{ {
completedBlock(image, error, cacheType); 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); objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} }

View File

@ -280,7 +280,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
[self.memCache setObject:diskImage forKey:key cost:cost]; [self.memCache setObject:diskImage forKey:key cost:cost];
} }
dispatch_async(dispatch_get_main_queue(), ^ dispatch_main_sync_safe(^
{ {
doneBlock(diskImage, SDImageCacheTypeDisk); doneBlock(diskImage, SDImageCacheTypeDisk);
}); });
@ -479,7 +479,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
if (completionBlock) if (completionBlock)
{ {
dispatch_async(dispatch_get_main_queue(), ^ dispatch_main_sync_safe(^
{ {
completionBlock(fileCount, totalSize); completionBlock(fileCount, totalSize);
}); });

View File

@ -42,3 +42,13 @@
#endif #endif
extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); 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);\
}

View File

@ -230,7 +230,7 @@
UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image];
image = [UIImage decodedImageWithImage:scaledImage]; image = [UIImage decodedImageWithImage:scaledImage];
CGImageRelease(partialImageRef); CGImageRelease(partialImageRef);
dispatch_async(dispatch_get_main_queue(), ^ dispatch_main_sync_safe(^
{ {
if (self.completedBlock) if (self.completedBlock)
{ {

View File

@ -163,7 +163,7 @@
{ {
UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; 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); completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished);
}); });

View File

@ -49,7 +49,7 @@ static char operationKey;
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{ {
if (!wself) return; if (!wself) return;
void (^block)(void) = ^ dispatch_main_sync_safe(^
{ {
__strong UIButton *sself = wself; __strong UIButton *sself = wself;
if (!sself) return; if (!sself) return;
@ -61,15 +61,7 @@ static char operationKey;
{ {
completedBlock(image, error, cacheType); 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); objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} }
@ -112,7 +104,7 @@ static char operationKey;
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{ {
if (!wself) return; if (!wself) return;
void (^block)(void) = ^ dispatch_main_sync_safe(^
{ {
__strong UIButton *sself = wself; __strong UIButton *sself = wself;
if (!sself) return; if (!sself) return;
@ -124,15 +116,7 @@ static char operationKey;
{ {
completedBlock(image, error, cacheType); 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); objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} }

View File

@ -56,7 +56,7 @@ static char operationArrayKey;
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{ {
if (!wself) return; if (!wself) return;
void (^block)(void) = ^ dispatch_main_sync_safe(^
{ {
__strong UIImageView *sself = wself; __strong UIImageView *sself = wself;
if (!sself) return; if (!sself) return;
@ -69,15 +69,7 @@ static char operationArrayKey;
{ {
completedBlock(image, error, cacheType); 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); objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} }
@ -95,7 +87,7 @@ static char operationArrayKey;
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{ {
if (!wself) return; if (!wself) return;
void (^block)(void) = ^ dispatch_main_sync_safe(^
{ {
__strong UIImageView *sself = wself; __strong UIImageView *sself = wself;
[sself stopAnimating]; [sself stopAnimating];
@ -112,15 +104,7 @@ static char operationArrayKey;
[sself setNeedsLayout]; [sself setNeedsLayout];
} }
[sself startAnimating]; [sself startAnimating];
}; });
if ([NSThread isMainThread])
{
block();
}
else
{
dispatch_sync(dispatch_get_main_queue(), block);
}
}]; }];
[operationsArray addObject:operation]; [operationsArray addObject:operation];
} }