Remove the `maxConcurrentDownloads`, which can be set from downloader configuration. Rename the `prefetcherQueue` to `delegateQueue` to match the correct description.

Fix the delegateQueue to async dispatch, avoid immediate callback cause recursion call(Match previous behavior)
This commit is contained in:
DreamPiggy 2018-03-31 17:33:18 +08:00
parent ea74f73834
commit 92f3d2c795
2 changed files with 16 additions and 23 deletions

View File

@ -58,15 +58,11 @@ typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls,
@interface SDWebImagePrefetcher : NSObject
/**
* The web image manager
* The web image manager used by prefetcher to prefetch images.
* @note You can specify a standalone manager and downloader with custom configuration suitable for image prefetching. Such as `currentDownloadCount` or `downloadTimeout`.
*/
@property (strong, nonatomic, readonly, nonnull) SDWebImageManager *manager;
/**
* Maximum number of URLs to prefetch at the same time. Defaults to 3.
*/
@property (nonatomic, assign) NSUInteger maxConcurrentDownloads;
/**
* The options for prefetcher. Defaults to SDWebImageLowPriority.
*/
@ -79,16 +75,18 @@ typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls,
/**
* Queue options for prefetcher when call the progressBlock, completionBlock and delegate methods. Defaults to Main Queue.
* @note The call is asynchronously to avoid blocking target queue.
* @note The delegate queue should be set before any prefetching start and may not be changed during prefetching to avoid thread-safe problem.
*/
@property (strong, nonatomic, nonnull) dispatch_queue_t prefetcherQueue;
@property (strong, nonatomic, nonnull) dispatch_queue_t delegateQueue;
/**
* The delegate for the prefetcher.
* The delegate for the prefetcher. Defatuls to nil.
*/
@property (weak, nonatomic, nullable) id <SDWebImagePrefetcherDelegate> delegate;
/**
* Returns the global shared image prefetcher instance.
* Returns the global shared image prefetcher instance. It use a standalone manager which is different from shared manager.
*/
@property (nonatomic, class, readonly, nonnull) SDWebImagePrefetcher *sharedImagePrefetcher;

View File

@ -51,20 +51,11 @@
_manager = manager;
_runningTokens = [NSMutableSet set];
_options = SDWebImageLowPriority;
_prefetcherQueue = dispatch_get_main_queue();
self.maxConcurrentDownloads = 3;
_delegateQueue = dispatch_get_main_queue();
}
return self;
}
- (void)setMaxConcurrentDownloads:(NSUInteger)maxConcurrentDownloads {
self.manager.imageDownloader.maxConcurrentDownloads = maxConcurrentDownloads;
}
- (NSUInteger)maxConcurrentDownloads {
return self.manager.imageDownloader.maxConcurrentDownloads;
}
#pragma mark - Prefetch
- (nullable SDWebImagePrefetchToken *)prefetchURLs:(nullable NSArray<NSURL *> *)urls {
return [self prefetchURLs:urls progress:nil completed:nil];
@ -138,9 +129,11 @@
return;
}
BOOL shouldCallDelegate = [self.delegate respondsToSelector:@selector(imagePrefetcher:didPrefetchURL:finishedCount:totalCount:)];
dispatch_queue_async_safe(self.prefetcherQueue, ^{
NSUInteger finishedCount = [self tokenFinishedCount];
NSUInteger totalCount = [self tokenTotalCount];
dispatch_async(self.delegateQueue, ^{
if (shouldCallDelegate) {
[self.delegate imagePrefetcher:self didPrefetchURL:url finishedCount:[self tokenFinishedCount] totalCount:[self tokenTotalCount]];
[self.delegate imagePrefetcher:self didPrefetchURL:url finishedCount:finishedCount totalCount:totalCount];
}
if (token.progressBlock) {
token.progressBlock((NSUInteger)token->_finishedCount, (NSUInteger)token.totalCount);
@ -153,9 +146,11 @@
return;
}
BOOL shoulCallDelegate = [self.delegate respondsToSelector:@selector(imagePrefetcher:didFinishWithTotalCount:skippedCount:)] && ([self countOfRunningTokens] == 1); // last one
dispatch_queue_async_safe(self.prefetcherQueue, ^{
NSUInteger totalCount = [self tokenTotalCount];
NSUInteger skippedCount = [self tokenSkippedCount];
dispatch_async(self.delegateQueue, ^{
if (shoulCallDelegate) {
[self.delegate imagePrefetcher:self didFinishWithTotalCount:[self tokenTotalCount] skippedCount:[self tokenSkippedCount]];
[self.delegate imagePrefetcher:self didFinishWithTotalCount:totalCount skippedCount:skippedCount];
}
if (token.completionBlock) {
token.completionBlock((NSUInteger)token->_finishedCount, (NSUInteger)token->_skippedCount);