Replacing #781 - made sure completionBlock called from `SDWebImageManager` is called on the main queue. Created 2 methods to simplify the code for calling the completions
This commit is contained in:
parent
265ace4a24
commit
0c47bc3023
|
@ -131,10 +131,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.absoluteString.length == 0 || (!(options & SDWebImageRetryFailed) && isFailedUrl)) {
|
if (url.absoluteString.length == 0 || (!(options & SDWebImageRetryFailed) && isFailedUrl)) {
|
||||||
dispatch_main_async_safe(^{
|
[self callCompletionBlockForOperation:operation completion:completedBlock error:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil] url:url];
|
||||||
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil];
|
|
||||||
completedBlock(nil, nil, error, SDImageCacheTypeNone, YES, url);
|
|
||||||
});
|
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,11 +148,9 @@
|
||||||
|
|
||||||
if ((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) {
|
if ((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) {
|
||||||
if (cachedImage && options & SDWebImageRefreshCached) {
|
if (cachedImage && options & SDWebImageRefreshCached) {
|
||||||
dispatch_main_async_safe(^{
|
// If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image
|
||||||
// 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.
|
||||||
// AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.
|
[self callCompletionBlockForOperation:weakOperation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
|
||||||
completedBlock(cachedImage, cachedData, nil, cacheType, YES, url);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// download if no image or requested to refresh anyway, and download allowed by delegate
|
// download if no image or requested to refresh anyway, and download allowed by delegate
|
||||||
|
@ -180,11 +175,7 @@
|
||||||
// See #699 for more details
|
// 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
|
// 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) {
|
} else if (error) {
|
||||||
dispatch_main_async_safe(^{
|
[self callCompletionBlockForOperation:strongOperation completion:completedBlock error:error url:url];
|
||||||
if (strongOperation && !strongOperation.isCancelled) {
|
|
||||||
completedBlock(nil, nil, error, SDImageCacheTypeNone, finished, url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if ( error.code != NSURLErrorNotConnectedToInternet
|
if ( error.code != NSURLErrorNotConnectedToInternet
|
||||||
&& error.code != NSURLErrorCancelled
|
&& error.code != NSURLErrorCancelled
|
||||||
|
@ -218,23 +209,14 @@
|
||||||
// pass nil if the image was transformed, so we can recalculate the data from the image
|
// pass nil if the image was transformed, so we can recalculate the data from the image
|
||||||
[self.imageCache storeImage:transformedImage imageData:(imageWasTransformed ? nil : downloadedData) forKey:key toDisk:cacheOnDisk completion:nil];
|
[self.imageCache storeImage:transformedImage imageData:(imageWasTransformed ? nil : downloadedData) forKey:key toDisk:cacheOnDisk completion:nil];
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_main_async_safe(^{
|
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
|
||||||
if (strongOperation && !strongOperation.isCancelled) {
|
|
||||||
completedBlock(transformedImage, downloadedData, nil, SDImageCacheTypeNone, finished, url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (downloadedImage && finished) {
|
if (downloadedImage && finished) {
|
||||||
[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil];
|
[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil];
|
||||||
}
|
}
|
||||||
|
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:downloadedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
|
||||||
dispatch_main_async_safe(^{
|
|
||||||
if (strongOperation && !strongOperation.isCancelled) {
|
|
||||||
completedBlock(downloadedImage, downloadedData, nil, SDImageCacheTypeNone, finished, url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,21 +230,13 @@
|
||||||
[self safelyRemoveOperationFromRunning:strongOperation];
|
[self safelyRemoveOperationFromRunning:strongOperation];
|
||||||
};
|
};
|
||||||
} else if (cachedImage) {
|
} else if (cachedImage) {
|
||||||
dispatch_main_async_safe(^{
|
__strong __typeof(weakOperation) strongOperation = weakOperation;
|
||||||
__strong __typeof(weakOperation) strongOperation = weakOperation;
|
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
|
||||||
if (strongOperation && !strongOperation.isCancelled) {
|
|
||||||
completedBlock(cachedImage, cachedData, nil, cacheType, YES, url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
[self safelyRemoveOperationFromRunning:operation];
|
[self safelyRemoveOperationFromRunning:operation];
|
||||||
} else {
|
} else {
|
||||||
// Image not in cache and download disallowed by delegate
|
// Image not in cache and download disallowed by delegate
|
||||||
dispatch_main_async_safe(^{
|
__strong __typeof(weakOperation) strongOperation = weakOperation;
|
||||||
__strong __typeof(weakOperation) strongOperation = weakOperation;
|
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:nil data:nil error:nil cacheType:SDImageCacheTypeNone finished:YES url:url];
|
||||||
if (strongOperation && !strongOperation.isCancelled) {
|
|
||||||
completedBlock(nil, nil, nil, SDImageCacheTypeNone, YES, url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
[self safelyRemoveOperationFromRunning:operation];
|
[self safelyRemoveOperationFromRunning:operation];
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
|
@ -301,6 +275,28 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)callCompletionBlockForOperation:(nullable SDWebImageCombinedOperation*)operation
|
||||||
|
completion:(nullable SDInternalCompletionBlock)completionBlock
|
||||||
|
error:(nullable NSError *)error
|
||||||
|
url:(nullable NSURL *)url {
|
||||||
|
[self callCompletionBlockForOperation:operation completion:completionBlock image:nil data:nil error:error cacheType:SDImageCacheTypeNone finished:YES url:url];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)callCompletionBlockForOperation:(nullable SDWebImageCombinedOperation*)operation
|
||||||
|
completion:(nullable SDInternalCompletionBlock)completionBlock
|
||||||
|
image:(nullable UIImage *)image
|
||||||
|
data:(nullable NSData *)data
|
||||||
|
error:(nullable NSError *)error
|
||||||
|
cacheType:(SDImageCacheType)cacheType
|
||||||
|
finished:(BOOL)finished
|
||||||
|
url:(nullable NSURL *)url {
|
||||||
|
dispatch_main_async_safe(^{
|
||||||
|
if (operation && !operation.isCancelled && completionBlock) {
|
||||||
|
completionBlock(image, data, error, cacheType, finished, url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue