Browse files

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

  • Loading branch information...
1 parent 9628a7d commit 10510baa73beb1484e7fc390275c2eed84372a87 @rs rs committed with Jack Tihon Feb 17, 2013
View
1 Examples/SDWebImage Demo/MasterViewController.m
@@ -333,6 +333,7 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
nil];
}
[SDWebImageManager.sharedManager.imageDownloader setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
+ SDWebImageManager.sharedManager.imageDownloader.queueMode = SDWebImageDownloaderLIFOQueueMode;
return self;
}
View
11 SDWebImage/SDWebImageDownloader.h
@@ -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
8 SDWebImage/SDWebImageDownloader.m
@@ -19,6 +19,7 @@
@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 @@ - (id)init
{
if ((self = [super init]))
{
+ _queueMode = SDWebImageDownloaderFILOQueueMode;
_downloadQueue = NSOperationQueue.new;
_downloadQueue.maxConcurrentOperationCount = 2;
_URLCallbacks = NSMutableDictionary.new;
@@ -156,6 +158,12 @@ - (NSInteger)maxConcurrentDownloads
[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;

0 comments on commit 10510ba

Please sign in to comment.