Skip to content

Commit

Permalink
Replacing #781 - replaced all the remaining dispatch_main_sync_safe
Browse files Browse the repository at this point in the history
… with `dispatch_main_async_safe`, so we no longer `dispatch_sync` on the main queue that can create issues.
  • Loading branch information
bpoplauschi committed Sep 30, 2016
1 parent 062e50a commit f7e8246
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 17 deletions.
7 changes: 0 additions & 7 deletions SDWebImage/SDWebImageCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ typedef void(^SDWebImageNoParamsBlock)();

extern NSString *const SDWebImageErrorDomain;

#define dispatch_main_sync_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_sync(dispatch_get_main_queue(), block);\
}

#define dispatch_main_async_safe(block)\
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {\
block();\
Expand Down
2 changes: 1 addition & 1 deletion SDWebImage/SDWebImageDownloaderOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data
image = scaledImage;
}
CGImageRelease(partialImageRef);
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
for (SDWebImageDownloaderCompletedBlock completedBlock in [self callbacksForKey:kCompletedCallbackKey]) {
completedBlock(image, nil, nil, NO);
}
Expand Down
14 changes: 7 additions & 7 deletions SDWebImage/SDWebImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ - (void)diskImageExistsForURL:(nullable NSURL *)url
}

if (url.absoluteString.length == 0 || (!(options & SDWebImageRetryFailed) && isFailedUrl)) {
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil];
completedBlock(nil, nil, error, SDImageCacheTypeNone, YES, url);
});
Expand All @@ -154,7 +154,7 @@ - (void)diskImageExistsForURL:(nullable NSURL *)url

if ((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) {
if (cachedImage && options & SDWebImageRefreshCached) {
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
// If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image
// AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.
completedBlock(cachedImage, cachedData, nil, cacheType, YES, url);
Expand Down Expand Up @@ -183,7 +183,7 @@ - (void)diskImageExistsForURL:(nullable NSURL *)url
// See #699 for more details
// if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data
} else if (error) {
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(nil, nil, error, SDImageCacheTypeNone, finished, url);
}
Expand Down Expand Up @@ -222,7 +222,7 @@ - (void)diskImageExistsForURL:(nullable NSURL *)url
[self.imageCache storeImage:transformedImage imageData:(imageWasTransformed ? nil : downloadedData) forKey:key toDisk:cacheOnDisk completion:nil];
}

dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(transformedImage, downloadedData, nil, SDImageCacheTypeNone, finished, url);
}
Expand All @@ -233,7 +233,7 @@ - (void)diskImageExistsForURL:(nullable NSURL *)url
[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil];
}

dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(downloadedImage, downloadedData, nil, SDImageCacheTypeNone, finished, url);
}
Expand All @@ -260,7 +260,7 @@ - (void)diskImageExistsForURL:(nullable NSURL *)url
}
};
} else if (cachedImage) {
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
__strong __typeof(weakOperation) strongOperation = weakOperation;
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(cachedImage, cachedData, nil, cacheType, YES, url);
Expand All @@ -271,7 +271,7 @@ - (void)diskImageExistsForURL:(nullable NSURL *)url
}
} else {
// Image not in cache and download disallowed by delegate
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
__strong __typeof(weakOperation) strongOperation = weakOperation;
if (strongOperation && !weakOperation.isCancelled) {
completedBlock(nil, nil, nil, SDImageCacheTypeNone, YES, url);
Expand Down
2 changes: 1 addition & 1 deletion SDWebImage/UIImageView+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ - (void)sd_setAnimationImagesWithURLs:(nonnull NSArray<NSURL *> *)arrayOfURLs {
for (NSURL *logoImageURL in arrayOfURLs) {
id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (!wself) return;
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
__strong UIImageView *sself = wself;
[sself stopAnimating];
if (sself && image) {
Expand Down
2 changes: 1 addition & 1 deletion SDWebImage/UIView+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ - (void)sd_internalSetImageWithURL:(nullable NSURL *)url
if (!sself) {
return;
}
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
if (!sself) {
return;
}
Expand Down

0 comments on commit f7e8246

Please sign in to comment.