Skip to content

Commit f024890

Browse files
author
Olivier Poitrey
committed
Add (kinda) LIFO queue mode support for image downloading (fix SDWebImage#294)
1 parent 350c0be commit f024890

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Examples/SDWebImage Demo/MasterViewController.m

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
333333
nil];
334334
}
335335
[SDWebImageManager.sharedManager.imageDownloader setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
336+
SDWebImageManager.sharedManager.imageDownloader.queueMode = SDWebImageDownloaderLIFOQueueMode;
336337
return self;
337338
}
338339

SDWebImage/SDWebImageDownloader.h

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ typedef enum
1616
SDWebImageDownloaderProgressiveDownload = 1 << 1
1717
} SDWebImageDownloaderOptions;
1818

19+
typedef enum
20+
{
21+
SDWebImageDownloaderFILOQueueMode,
22+
SDWebImageDownloaderLIFOQueueMode
23+
} SDWebImageDownloaderQueueMode;
24+
1925
extern NSString *const SDWebImageDownloadStartNotification;
2026
extern NSString *const SDWebImageDownloadStopNotification;
2127

@@ -29,6 +35,11 @@ typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data,
2935

3036
@property (assign, nonatomic) NSInteger maxConcurrentDownloads;
3137

38+
/**
39+
* Changes download operations unqueue mode. Default value is `SDWebImageDownloaderFILOQueueMode`.
40+
*/
41+
@property (assign, nonatomic) SDWebImageDownloaderQueueMode queueMode;
42+
3243
+ (SDWebImageDownloader *)sharedDownloader;
3344

3445
/**

SDWebImage/SDWebImageDownloader.m

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
@interface SDWebImageDownloader ()
2020

2121
@property (strong, nonatomic) NSOperationQueue *downloadQueue;
22+
@property (weak, nonatomic) NSOperation *lastAddedOperation;
2223
@property (strong, nonatomic) NSMutableDictionary *URLCallbacks;
2324
@property (strong, nonatomic) NSMutableDictionary *HTTPHeaders;
2425
// This queue is used to serialize the handling of the network responses of all the download operation in a single queue
@@ -66,6 +67,7 @@ - (id)init
6667
{
6768
if ((self = [super init]))
6869
{
70+
_queueMode = SDWebImageDownloaderFILOQueueMode;
6971
_downloadQueue = NSOperationQueue.new;
7072
_downloadQueue.maxConcurrentOperationCount = 2;
7173
_URLCallbacks = NSMutableDictionary.new;
@@ -156,6 +158,12 @@ - (NSInteger)maxConcurrentDownloads
156158
[sself removeCallbacksForURL:url];
157159
}];
158160
[wself.downloadQueue addOperation:operation];
161+
if (wself.queueMode == SDWebImageDownloaderLIFOQueueMode)
162+
{
163+
// Emulate LIFO queue mode by systematically adding new operations as last operation's dependency
164+
[wself.lastAddedOperation addDependency:operation];
165+
wself.lastAddedOperation = operation;
166+
}
159167
}];
160168

161169
return operation;

0 commit comments

Comments
 (0)