Add new method to SDWebImagePrefetcher to support a completion block #127

Closed
wants to merge 1 commit into from
View
8 SDWebImage/SDWebImagePrefetcher.h
@@ -10,6 +10,10 @@
#import "SDWebImageManagerDelegate.h"
#import "SDWebImageManager.h"
+#if NS_BLOCKS_AVAILABLE
+typedef void (^SDWebImagePrefetcherCompletion)(NSUInteger finishedCount, NSUInteger skippedCount);
+#endif
+
/**
* Prefetch some URLs in the cache for future use. Images are downloaded in low priority.
*/
@@ -46,7 +50,9 @@
* @param urls list of URLs to prefetch
*/
- (void)prefetchURLs:(NSArray *)urls;
-
+#if NS_BLOCKS_AVAILABLE
+- (void)prefetchURLs:(NSArray *)urls completion:(SDWebImagePrefetcherCompletion)completion;
+#endif
/**
* Remove and cancel queued list
View
35 SDWebImage/SDWebImagePrefetcher.m
@@ -10,7 +10,15 @@
#import "SDWebImageManager.h"
@interface SDWebImagePrefetcher ()
+{
+#if NS_BLOCKS_AVAILABLE
+ SDWebImagePrefetcherCompletion _completion;
+#endif
+}
@property (nonatomic, retain) NSArray *prefetchURLs;
+#if NS_BLOCKS_AVAILABLE
+@property (nonatomic, copy) SDWebImagePrefetcherCompletion completion;
+#endif
@end
@implementation SDWebImagePrefetcher
@@ -20,6 +28,9 @@ @implementation SDWebImagePrefetcher
@synthesize prefetchURLs;
@synthesize maxConcurrentDownloads;
@synthesize options;
+#if NS_BLOCKS_AVAILABLE
+@synthesize completion = _completion;
+#endif
+ (SDWebImagePrefetcher *)sharedImagePrefetcher
{
@@ -61,6 +72,15 @@ - (void)prefetchURLs:(NSArray *)urls
}
}
+#if NS_BLOCKS_AVAILABLE
+- (void)prefetchURLs:(NSArray *)urls completion:(SDWebImagePrefetcherCompletion)completion
+{
+ self.completion = completion;
+ [self prefetchURLs:urls];
+}
+#endif
+
+
- (void)cancelPrefetching
{
self.prefetchURLs = nil;
@@ -84,6 +104,12 @@ - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UI
else if (_finishedCount == _requestedCount)
{
[self reportStatus];
+#if NS_BLOCKS_AVAILABLE
+ if(self.completion != nil) {
+ self.completion(_finishedCount, _skippedCount);
+ self.completion = nil;
+ }
+#endif
}
}
@@ -102,12 +128,21 @@ - (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSEr
else if (_finishedCount == _requestedCount)
{
[self reportStatus];
+#if NS_BLOCKS_AVAILABLE
+ if(self.completion != nil) {
+ self.completion(_finishedCount, _skippedCount);
+ self.completion = nil;
+ }
+#endif
}
}
- (void)dealloc
{
self.prefetchURLs = nil;
+#if NS_BLOCKS_AVAILABLE
+ self.completion = nil;
+#endif
SDWISuperDealoc;
}