From 1ac8c23723a1b2eac13562fbedcdbf3cac77d014 Mon Sep 17 00:00:00 2001 From: Sash Zats Date: Wed, 24 Aug 2016 14:06:12 -0700 Subject: [PATCH] Allow to specify NSURLSessionConfiguration for SDWebImageDownloader --- SDWebImage/SDWebImageDownloader.h | 9 ++++++++- SDWebImage/SDWebImageDownloader.m | 13 ++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.h b/SDWebImage/SDWebImageDownloader.h index d440e043..ae4af26b 100644 --- a/SDWebImage/SDWebImageDownloader.h +++ b/SDWebImage/SDWebImageDownloader.h @@ -107,7 +107,7 @@ typedef NSDictionary *(^SDWebImageDownloaderHeadersFilterBlock)(NSURL *url, NSDi * * @return global shared instance of downloader class */ -+ (SDWebImageDownloader *)sharedDownloader; ++ (instancetype)sharedDownloader; /** * Set the default URL credential to be set for request operations. @@ -132,6 +132,13 @@ typedef NSDictionary *(^SDWebImageDownloaderHeadersFilterBlock)(NSURL *url, NSDi */ @property (nonatomic, copy) SDWebImageDownloaderHeadersFilterBlock headersFilter; +/** + * Creates an instance of a downloader with specified session configuration. + * *Note*: `timeoutIntervalForRequest` is going to be overwritten. + * @return new instance of downloader class + */ +- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration NS_DESIGNATED_INITIALIZER; + /** * Set a value for a HTTP header to be appended to each download HTTP request. * diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index ca703033..8deb2474 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -53,7 +53,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; } } -+ (SDWebImageDownloader *)sharedDownloader { ++ (instancetype)sharedDownloader { static dispatch_once_t once; static id instance; dispatch_once(&once, ^{ @@ -62,7 +62,11 @@ static NSString *const kCompletedCallbackKey = @"completed"; return instance; } -- (id)init { +- (instancetype)init { + return [self initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; +} + +- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration { if ((self = [super init])) { _operationClass = [SDWebImageDownloaderOperation class]; _shouldDecompressImages = YES; @@ -78,15 +82,14 @@ static NSString *const kCompletedCallbackKey = @"completed"; _barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT); _downloadTimeout = 15.0; - NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; - sessionConfig.timeoutIntervalForRequest = _downloadTimeout; + sessionConfiguration.timeoutIntervalForRequest = _downloadTimeout; /** * Create the session for this task * We send nil as delegate queue so that the session creates a serial operation queue for performing all delegate * method calls and completion handler calls. */ - self.session = [NSURLSession sessionWithConfiguration:sessionConfig + self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil]; }