Add (kinda) LIFO queue mode support for image downloading (fix #294)

This commit is contained in:
Olivier Poitrey 2013-02-17 03:02:04 +01:00
parent 350c0bea9a
commit f024890ba7
3 changed files with 20 additions and 0 deletions

View File

@ -333,6 +333,7 @@
nil];
}
[SDWebImageManager.sharedManager.imageDownloader setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
SDWebImageManager.sharedManager.imageDownloader.queueMode = SDWebImageDownloaderLIFOQueueMode;
return self;
}

View File

@ -16,6 +16,12 @@ typedef enum
SDWebImageDownloaderProgressiveDownload = 1 << 1
} SDWebImageDownloaderOptions;
typedef enum
{
SDWebImageDownloaderFILOQueueMode,
SDWebImageDownloaderLIFOQueueMode
} SDWebImageDownloaderQueueMode;
extern NSString *const SDWebImageDownloadStartNotification;
extern NSString *const SDWebImageDownloadStopNotification;
@ -29,6 +35,11 @@ typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data,
@property (assign, nonatomic) NSInteger maxConcurrentDownloads;
/**
* Changes download operations unqueue mode. Default value is `SDWebImageDownloaderFILOQueueMode`.
*/
@property (assign, nonatomic) SDWebImageDownloaderQueueMode queueMode;
+ (SDWebImageDownloader *)sharedDownloader;
/**

View File

@ -19,6 +19,7 @@ static NSString *const kCompletedCallbackKey = @"completed";
@interface SDWebImageDownloader ()
@property (strong, nonatomic) NSOperationQueue *downloadQueue;
@property (weak, nonatomic) NSOperation *lastAddedOperation;
@property (strong, nonatomic) NSMutableDictionary *URLCallbacks;
@property (strong, nonatomic) NSMutableDictionary *HTTPHeaders;
// 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 @@ static NSString *const kCompletedCallbackKey = @"completed";
{
if ((self = [super init]))
{
_queueMode = SDWebImageDownloaderFILOQueueMode;
_downloadQueue = NSOperationQueue.new;
_downloadQueue.maxConcurrentOperationCount = 2;
_URLCallbacks = NSMutableDictionary.new;
@ -156,6 +158,12 @@ static NSString *const kCompletedCallbackKey = @"completed";
[sself removeCallbacksForURL:url];
}];
[wself.downloadQueue addOperation:operation];
if (wself.queueMode == SDWebImageDownloaderLIFOQueueMode)
{
// Emulate LIFO queue mode by systematically adding new operations as last operation's dependency
[wself.lastAddedOperation addDependency:operation];
wself.lastAddedOperation = operation;
}
}];
return operation;