Skip to content

Commit 350c0be

Browse files
author
Olivier Poitrey
committed
Add completion block support to SDWebImagePrefetcher (fix SDWebImage#127)
1 parent ebd63a8 commit 350c0be

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

SDWebImage/SDWebImagePrefetcher.h

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
*/
4040
- (void)prefetchURLs:(NSArray *)urls;
4141

42+
/**
43+
* Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching,
44+
* currently one image is downloaded at a time,
45+
* and skips images for failed downloads and proceed to the next image in the list
46+
*
47+
* @param urls list of URLs to prefetch
48+
* @param completionBlock block to be called when prefetching is completed
49+
*/
50+
- (void)prefetchURLs:(NSArray *)urls completed:(void (^)(NSUInteger finishedCount, NSUInteger skippedCount))completionBlock;
4251

4352
/**
4453
* Remove and cancel queued list

SDWebImage/SDWebImagePrefetcher.m

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ @interface SDWebImagePrefetcher ()
1717
@property (assign, nonatomic) NSUInteger skippedCount;
1818
@property (assign, nonatomic) NSUInteger finishedCount;
1919
@property (assign, nonatomic) NSTimeInterval startedTime;
20+
@property (SDDispatchQueueSetterSementics, nonatomic) void (^completionBlock)(NSUInteger, NSUInteger);
2021

2122
@end
2223

@@ -79,6 +80,11 @@ - (void)startPrefetchingAtIndex:(NSUInteger)index
7980
else if (self.finishedCount == self.requestedCount)
8081
{
8182
[self reportStatus];
83+
if (self.completionBlock)
84+
{
85+
self.completionBlock(self.finishedCount, self.skippedCount);
86+
self.completionBlock = nil;
87+
}
8288
}
8389
}];
8490
}
@@ -90,10 +96,16 @@ - (void)reportStatus
9096
}
9197

9298
- (void)prefetchURLs:(NSArray *)urls
99+
{
100+
[self prefetchURLs:urls completed:nil];
101+
}
102+
103+
- (void)prefetchURLs:(NSArray *)urls completed:(void (^)(NSUInteger, NSUInteger))completionBlock
93104
{
94105
[self cancelPrefetching]; // Prevent duplicate prefetch request
95106
self.startedTime = CFAbsoluteTimeGetCurrent();
96107
self.prefetchURLs = urls;
108+
self.completionBlock = completionBlock;
97109

98110
// Starts prefetching from the very first image on the list with the max allowed concurrency
99111
NSUInteger listCount = self.prefetchURLs.count;

0 commit comments

Comments
 (0)