Add low priority download mode. Low priorities run NSUrlConnection in NSEventTrackingRunLoopMode
This commit is contained in:
parent
44fd72843b
commit
34e8082cde
|
@ -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;
|
||||||
|
|
|
@ -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"))
|
||||||
|
@ -40,11 +45,12 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
|
||||||
selector:NSSelectorFromString(@"stopActivity")
|
selector:NSSelectorFromString(@"stopActivity")
|
||||||
name:SDWebImageDownloadStopNotification object:nil];
|
name:SDWebImageDownloadStopNotification object:nil];
|
||||||
}
|
}
|
||||||
|
|
||||||
SDWebImageDownloader *downloader = [[[SDWebImageDownloader alloc] init] autorelease];
|
SDWebImageDownloader *downloader = [[[SDWebImageDownloader alloc] init] autorelease];
|
||||||
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];
|
||||||
|
|
||||||
|
@ -106,7 +116,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
|
||||||
{
|
{
|
||||||
[delegate performSelector:@selector(imageDownloaderDidFinish:) withObject:self];
|
[delegate performSelector:@selector(imageDownloaderDidFinish:) withObject:self];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
|
if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
|
||||||
{
|
{
|
||||||
UIImage *image = [[UIImage alloc] initWithData:imageData];
|
UIImage *image = [[UIImage alloc] initWithData:imageData];
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -60,15 +60,20 @@ 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]))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate
|
- (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue