Authored by Bogdan Poplauschi

Replacing #781 - replaced all the remaining `dispatch_main_sync_safe` with `disp…

…atch_main_async_safe`, so we no longer `dispatch_sync` on the main queue that can create issues.
... ... @@ -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();\
... ...
... ... @@ -372,7 +372,7 @@ didReceiveResponse:(NSURLResponse *)response
image = scaledImage;
}
CGImageRelease(partialImageRef);
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
for (SDWebImageDownloaderCompletedBlock completedBlock in [self callbacksForKey:kCompletedCallbackKey]) {
completedBlock(image, nil, nil, NO);
}
... ...
... ... @@ -131,7 +131,7 @@
}
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);
});
... ... @@ -154,7 +154,7 @@
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);
... ... @@ -183,7 +183,7 @@
// 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);
}
... ... @@ -222,7 +222,7 @@
[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);
}
... ... @@ -233,7 +233,7 @@
[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);
}
... ... @@ -260,7 +260,7 @@
}
};
} 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);
... ... @@ -271,7 +271,7 @@
}
} 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);
... ...
... ... @@ -78,7 +78,7 @@
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) {
... ...
... ... @@ -57,7 +57,7 @@ static char TAG_ACTIVITY_SHOW;
if (!sself) {
return;
}
dispatch_main_sync_safe(^{
dispatch_main_async_safe(^{
if (!sself) {
return;
}
... ...