Skip to content

Commit

Permalink
Call handlers on the main thread synchronously to enhance responsivity (
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Poitrey authored and devedup committed Sep 10, 2014
1 parent 4177643 commit 2462000
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 93 deletions.
12 changes: 2 additions & 10 deletions SDWebImage/MKAnnotationView+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
id<SDWebImageOperation> 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;
Expand All @@ -62,15 +62,7 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
{
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);
}
Expand Down
4 changes: 2 additions & 2 deletions SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ - (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDIm
[self.memCache setObject:diskImage forKey:key cost:cost];
}

dispatch_async(dispatch_get_main_queue(), ^
dispatch_main_sync_safe(^
{
doneBlock(diskImage, SDImageCacheTypeDisk);
});
Expand Down Expand Up @@ -479,7 +479,7 @@ - (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, unsigne

if (completionBlock)
{
dispatch_async(dispatch_get_main_queue(), ^
dispatch_main_sync_safe(^
{
completionBlock(fileCount, totalSize);
});
Expand Down
12 changes: 11 additions & 1 deletion SDWebImage/SDWebImageCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@
#define SDDispatchQueueSetterSementics assign
#endif

extern 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);\
}
2 changes: 1 addition & 1 deletion SDWebImage/SDWebImageDownloaderOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
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)
{
Expand Down
2 changes: 1 addition & 1 deletion SDWebImage/SDWebImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ - (NSString *)cacheKeyForURL:(NSURL *)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);
});
Expand Down
24 changes: 4 additions & 20 deletions SDWebImage/UIButton+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderI
id<SDWebImageOperation> 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;
Expand All @@ -61,15 +61,7 @@ - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderI
{
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);
}
Expand Down Expand Up @@ -112,7 +104,7 @@ - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state pl
id<SDWebImageOperation> 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;
Expand All @@ -124,15 +116,7 @@ - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state pl
{
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);
}
Expand Down
105 changes: 47 additions & 58 deletions SDWebImage/UIImageView+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
id<SDWebImageOperation> 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;
Expand All @@ -69,58 +69,47 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
{
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);
}
}

-(void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs{
[self cancelCurrentArrayLoad];
__weak UIImageView *wself = self;

NSMutableArray *operationsArray = [[NSMutableArray alloc] init];

for(NSURL *logoImageURL in arrayOfURLs){
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){
if (!wself) return;
void (^block)(void) = ^
{
__strong UIImageView *sself = wself;
[sself stopAnimating];
if (!sself) return;
if (image)
{
NSMutableArray *currentImages = [[sself animationImages] mutableCopy];
if(!currentImages) currentImages = [[NSMutableArray alloc] init];
[currentImages addObject:image];

sself.animationImages = currentImages;
[sself setNeedsLayout];
}
[sself startAnimating];
};
if ([NSThread isMainThread])
{
block();
}
else
{
dispatch_sync(dispatch_get_main_queue(), block);
}
}];
[operationsArray addObject:operation];
}

objc_setAssociatedObject(self, &operationArrayKey, [NSArray arrayWithArray:operationsArray], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs
{
[self cancelCurrentArrayLoad];
__weak UIImageView *wself = self;

NSMutableArray *operationsArray = [[NSMutableArray alloc] init];

for (NSURL *logoImageURL in arrayOfURLs)
{
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (!wself) return;
dispatch_main_sync_safe(^
{
__strong UIImageView *sself = wself;
[sself stopAnimating];
if (sself && image)
{
NSMutableArray *currentImages = [[sself animationImages] mutableCopy];
if (!currentImages)
{
currentImages = [[NSMutableArray alloc] init];
}
[currentImages addObject:image];

sself.animationImages = currentImages;
[sself setNeedsLayout];
}
[sself startAnimating];
});
}];
[operationsArray addObject:operation];
}

objc_setAssociatedObject(self, &operationArrayKey, [NSArray arrayWithArray:operationsArray], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)cancelCurrentImageLoad
Expand All @@ -136,16 +125,16 @@ - (void)cancelCurrentImageLoad

- (void)cancelCurrentArrayLoad
{
// Cancel in progress downloader from queue
NSArray *operations = objc_getAssociatedObject(self, &operationArrayKey);
for(id<SDWebImageOperation> operation in operations){
if (operation)
{
[operation cancel];
}
}
objc_setAssociatedObject(self, &operationArrayKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// Cancel in progress downloader from queue
NSArray *operations = objc_getAssociatedObject(self, &operationArrayKey);
for (id<SDWebImageOperation> operation in operations)
{
if (operation)
{
[operation cancel];
}
}
objc_setAssociatedObject(self, &operationArrayKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

0 comments on commit 2462000

Please sign in to comment.