Add low priority download mode. Low priorities run NSUrlConnection in NSEventTrackingRunLoopMode

This commit is contained in:
Jeppe Vesterbæk 2011-05-06 10:30:50 +02:00
parent 44fd72843b
commit 34e8082cde
4 changed files with 34 additions and 10 deletions

View File

@ -21,13 +21,16 @@ extern NSString *const SDWebImageDownloadStopNotification;
NSURLConnection *connection; NSURLConnection *connection;
NSMutableData *imageData; NSMutableData *imageData;
id userInfo; id userInfo;
BOOL lowPriority;
} }
@property (nonatomic, retain) NSURL *url; @property (nonatomic, retain) NSURL *url;
@property (nonatomic, assign) id<SDWebImageDownloaderDelegate> delegate; @property (nonatomic, assign) id<SDWebImageDownloaderDelegate> delegate;
@property (nonatomic, retain) NSMutableData *imageData; @property (nonatomic, retain) NSMutableData *imageData;
@property (nonatomic, retain) id userInfo; @property (nonatomic, retain) id userInfo;
@property (nonatomic, readwrite) BOOL lowPriority;
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo lowPriority:(BOOL)lowPriority;
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo; + (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo;
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate; + (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate;
- (void)start; - (void)start;

View File

@ -16,7 +16,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
@end @end
@implementation SDWebImageDownloader @implementation SDWebImageDownloader
@synthesize url, delegate, connection, imageData, userInfo; @synthesize url, delegate, connection, imageData, userInfo, lowPriority;
#pragma mark Public Methods #pragma mark Public Methods
@ -28,6 +28,11 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo + (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo
{ {
return [[self class] downloaderWithURL:url delegate:delegate userInfo:userInfo lowPriority:NO];
}
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo lowPriority:(BOOL)lowPriority
{
// Bind SDNetworkActivityIndicator if available (download it here: http://github.com/rs/SDNetworkActivityIndicator ) // Bind SDNetworkActivityIndicator if available (download it here: http://github.com/rs/SDNetworkActivityIndicator )
// To use it, just add #import "SDNetworkActivityIndicator.h" in addition to the SDWebImage import // To use it, just add #import "SDNetworkActivityIndicator.h" in addition to the SDWebImage import
if (NSClassFromString(@"SDNetworkActivityIndicator")) if (NSClassFromString(@"SDNetworkActivityIndicator"))
@ -45,6 +50,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
downloader.url = url; downloader.url = url;
downloader.delegate = delegate; downloader.delegate = delegate;
downloader.userInfo = userInfo; downloader.userInfo = userInfo;
downloader.lowPriority = lowPriority;
[downloader performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES]; [downloader performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES];
return downloader; return downloader;
} }
@ -59,8 +65,12 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
// In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease]; self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
// Ensure we aren't blocked by UI manipulations (default runloop mode for NSURLConnection is NSEventTrackingRunLoopMode)
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; // If not in low priority mode, ensure we aren't blocked by UI manipulations (default runloop mode for NSURLConnection is NSEventTrackingRunLoopMode)
if (!lowPriority)
{
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
[connection start]; [connection start];
[request release]; [request release];

View File

@ -23,6 +23,7 @@
- (UIImage *)imageWithURL:(NSURL *)url; - (UIImage *)imageWithURL:(NSURL *)url;
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate; - (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate;
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed; - (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed;
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority;
- (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate; - (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate;
@end @end

View File

@ -60,6 +60,11 @@ static SDWebImageManager *instance;
} }
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed - (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed
{
[self downloadWithURL:url delegate:delegate retryFailed:retryFailed lowPriority:NO];
}
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority
{ {
if (!url || !delegate || (!retryFailed && [failedURLs containsObject:url])) if (!url || !delegate || (!retryFailed && [failedURLs containsObject:url]))
{ {
@ -67,7 +72,7 @@ static SDWebImageManager *instance;
} }
// Check the on-disk cache async so we don't block the main thread // Check the on-disk cache async so we don't block the main thread
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", nil]; NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", [NSNumber numberWithBool:lowPriority], @"low_priority", nil];
[[SDImageCache sharedImageCache] queryDiskCacheForKey:[url absoluteString] delegate:self userInfo:info]; [[SDImageCache sharedImageCache] queryDiskCacheForKey:[url absoluteString] delegate:self userInfo:info];
} }
@ -110,16 +115,21 @@ static SDWebImageManager *instance;
{ {
NSURL *url = [info objectForKey:@"url"]; NSURL *url = [info objectForKey:@"url"];
id<SDWebImageManagerDelegate> delegate = [info objectForKey:@"delegate"]; id<SDWebImageManagerDelegate> delegate = [info objectForKey:@"delegate"];
BOOL lowPriority = [[info objectForKey:@"low_priority"] boolValue];
// Share the same downloader for identical URLs so we don't download the same URL several times // Share the same downloader for identical URLs so we don't download the same URL several times
SDWebImageDownloader *downloader = [downloaderForURL objectForKey:url]; SDWebImageDownloader *downloader = [downloaderForURL objectForKey:url];
if (!downloader) if (!downloader)
{ {
downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self]; downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self userInfo:nil lowPriority:lowPriority];
[downloaderForURL setObject:downloader forKey:url]; [downloaderForURL setObject:downloader forKey:url];
} }
// If we get a normal priority request, make sure to change type since downloader is shared
if (!lowPriority && downloader.lowPriority)
downloader.lowPriority = NO;
[delegates addObject:delegate]; [delegates addObject:delegate];
[downloaders addObject:downloader]; [downloaders addObject:downloader];
} }