Skip to content

Commit 125515d

Browse files
committed
Merge pull request SDWebImage#876 from matias-pequeno/custom_operations
Allows user to override default SDWebImageDownloaderOperation
2 parents a4c213e + 3671cdd commit 125515d

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

SDWebImage/SDWebImageDownloader.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ typedef NSDictionary *(^SDWebImageDownloaderHeadersFilterBlock)(NSURL *url, NSDi
139139
*/
140140
- (NSString *)valueForHTTPHeaderField:(NSString *)field;
141141

142+
/**
143+
* Sets a subclass of `SDWebImageDownloaderOperation` as the default
144+
* `NSOperation` to be used each time SDWebImage constructs a request
145+
* operation to download an image.
146+
*
147+
* @param operationClass The subclass of `SDWebImageDownloaderOperation` to set
148+
* as default. Passing `nil` will revert to `SDWebImageDownloaderOperation`.
149+
*/
150+
- (void)setOperationClass:(Class)operationClass;
151+
142152
/**
143153
* Creates a SDWebImageDownloader async downloader instance with a given URL
144154
*

SDWebImage/SDWebImageDownloader.m

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ @interface SDWebImageDownloader ()
2020

2121
@property (strong, nonatomic) NSOperationQueue *downloadQueue;
2222
@property (weak, nonatomic) NSOperation *lastAddedOperation;
23+
@property (assign, nonatomic) Class operationClass;
2324
@property (strong, nonatomic) NSMutableDictionary *URLCallbacks;
2425
@property (strong, nonatomic) NSMutableDictionary *HTTPHeaders;
2526
// This queue is used to serialize the handling of the network responses of all the download operation in a single queue
@@ -63,6 +64,7 @@ + (SDWebImageDownloader *)sharedDownloader {
6364

6465
- (id)init {
6566
if ((self = [super init])) {
67+
_operationClass = [SDWebImageDownloaderOperation class];
6668
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
6769
_downloadQueue = [NSOperationQueue new];
6870
_downloadQueue.maxConcurrentOperationCount = 2;
@@ -104,6 +106,10 @@ - (NSInteger)maxConcurrentDownloads {
104106
return _downloadQueue.maxConcurrentOperationCount;
105107
}
106108

109+
- (void)setOperationClass:(Class)operationClass {
110+
_operationClass = operationClass ?: [SDWebImageDownloaderOperation class];
111+
}
112+
107113
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
108114
__block SDWebImageDownloaderOperation *operation;
109115
__weak SDWebImageDownloader *wself = self;
@@ -124,34 +130,34 @@ - (NSInteger)maxConcurrentDownloads {
124130
else {
125131
request.allHTTPHeaderFields = wself.HTTPHeaders;
126132
}
127-
operation = [[SDWebImageDownloaderOperation alloc] initWithRequest:request
128-
options:options
129-
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
130-
SDWebImageDownloader *sself = wself;
131-
if (!sself) return;
132-
NSArray *callbacksForURL = [sself callbacksForURL:url];
133-
for (NSDictionary *callbacks in callbacksForURL) {
134-
SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
135-
if (callback) callback(receivedSize, expectedSize);
136-
}
137-
}
138-
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
139-
SDWebImageDownloader *sself = wself;
140-
if (!sself) return;
141-
NSArray *callbacksForURL = [sself callbacksForURL:url];
142-
if (finished) {
143-
[sself removeCallbacksForURL:url];
144-
}
145-
for (NSDictionary *callbacks in callbacksForURL) {
146-
SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
147-
if (callback) callback(image, data, error, finished);
148-
}
149-
}
150-
cancelled:^{
151-
SDWebImageDownloader *sself = wself;
152-
if (!sself) return;
153-
[sself removeCallbacksForURL:url];
154-
}];
133+
operation = [[wself.operationClass alloc] initWithRequest:request
134+
options:options
135+
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
136+
SDWebImageDownloader *sself = wself;
137+
if (!sself) return;
138+
NSArray *callbacksForURL = [sself callbacksForURL:url];
139+
for (NSDictionary *callbacks in callbacksForURL) {
140+
SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
141+
if (callback) callback(receivedSize, expectedSize);
142+
}
143+
}
144+
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
145+
SDWebImageDownloader *sself = wself;
146+
if (!sself) return;
147+
NSArray *callbacksForURL = [sself callbacksForURL:url];
148+
if (finished) {
149+
[sself removeCallbacksForURL:url];
150+
}
151+
for (NSDictionary *callbacks in callbacksForURL) {
152+
SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
153+
if (callback) callback(image, data, error, finished);
154+
}
155+
}
156+
cancelled:^{
157+
SDWebImageDownloader *sself = wself;
158+
if (!sself) return;
159+
[sself removeCallbacksForURL:url];
160+
}];
155161

156162
if (wself.username && wself.password) {
157163
operation.credential = [NSURLCredential credentialWithUser:wself.username password:wself.password persistence:NSURLCredentialPersistenceForSession];

0 commit comments

Comments
 (0)