From 350c0bea9af7af68ea7ef8bde58e40d6aa8b033a Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Sun, 17 Feb 2013 02:43:46 +0100 Subject: [PATCH] Add completion block support to `SDWebImagePrefetcher` (fix #127) --- SDWebImage/SDWebImagePrefetcher.h | 9 +++++++++ SDWebImage/SDWebImagePrefetcher.m | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/SDWebImage/SDWebImagePrefetcher.h b/SDWebImage/SDWebImagePrefetcher.h index 526072f2..7695f570 100644 --- a/SDWebImage/SDWebImagePrefetcher.h +++ b/SDWebImage/SDWebImagePrefetcher.h @@ -39,6 +39,15 @@ */ - (void)prefetchURLs:(NSArray *)urls; +/** + * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching, + * currently one image is downloaded at a time, + * and skips images for failed downloads and proceed to the next image in the list + * + * @param urls list of URLs to prefetch + * @param completionBlock block to be called when prefetching is completed + */ +- (void)prefetchURLs:(NSArray *)urls completed:(void (^)(NSUInteger finishedCount, NSUInteger skippedCount))completionBlock; /** * Remove and cancel queued list diff --git a/SDWebImage/SDWebImagePrefetcher.m b/SDWebImage/SDWebImagePrefetcher.m index 73645776..e1c188c9 100644 --- a/SDWebImage/SDWebImagePrefetcher.m +++ b/SDWebImage/SDWebImagePrefetcher.m @@ -17,6 +17,7 @@ @property (assign, nonatomic) NSUInteger skippedCount; @property (assign, nonatomic) NSUInteger finishedCount; @property (assign, nonatomic) NSTimeInterval startedTime; +@property (SDDispatchQueueSetterSementics, nonatomic) void (^completionBlock)(NSUInteger, NSUInteger); @end @@ -79,6 +80,11 @@ else if (self.finishedCount == self.requestedCount) { [self reportStatus]; + if (self.completionBlock) + { + self.completionBlock(self.finishedCount, self.skippedCount); + self.completionBlock = nil; + } } }]; } @@ -90,10 +96,16 @@ } - (void)prefetchURLs:(NSArray *)urls +{ + [self prefetchURLs:urls completed:nil]; +} + +- (void)prefetchURLs:(NSArray *)urls completed:(void (^)(NSUInteger, NSUInteger))completionBlock { [self cancelPrefetching]; // Prevent duplicate prefetch request self.startedTime = CFAbsoluteTimeGetCurrent(); self.prefetchURLs = urls; + self.completionBlock = completionBlock; // Starts prefetching from the very first image on the list with the max allowed concurrency NSUInteger listCount = self.prefetchURLs.count;