Remove async calls from SDWebImageDownloader. The operation is already run in a background thread also the async calls are unnecessary.

This commit is contained in:
Charlie Savage 2013-04-26 21:43:11 -06:00
parent b85556fa86
commit d53a47e982
3 changed files with 98 additions and 122 deletions

View File

@ -23,7 +23,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
@property (strong, nonatomic) NSMutableDictionary *URLCallbacks; @property (strong, nonatomic) NSMutableDictionary *URLCallbacks;
@property (strong, nonatomic) NSMutableDictionary *HTTPHeaders; @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 // This queue is used to serialize the handling of the network responses of all the download operation in a single queue
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t workingQueue;
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t barrierQueue; @property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t barrierQueue;
@end @end
@ -72,7 +71,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
_downloadQueue.maxConcurrentOperationCount = 2; _downloadQueue.maxConcurrentOperationCount = 2;
_URLCallbacks = NSMutableDictionary.new; _URLCallbacks = NSMutableDictionary.new;
_HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/*" forKey:@"Accept"]; _HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/*" forKey:@"Accept"];
_workingQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloader", DISPATCH_QUEUE_SERIAL);
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT); _barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
} }
return self; return self;
@ -81,7 +79,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
- (void)dealloc - (void)dealloc
{ {
[self.downloadQueue cancelAllOperations]; [self.downloadQueue cancelAllOperations];
SDDispatchQueueRelease(_workingQueue);
SDDispatchQueueRelease(_barrierQueue); SDDispatchQueueRelease(_barrierQueue);
} }
@ -124,7 +121,7 @@ static NSString *const kCompletedCallbackKey = @"completed";
request.HTTPShouldHandleCookies = NO; request.HTTPShouldHandleCookies = NO;
request.HTTPShouldUsePipelining = YES; request.HTTPShouldUsePipelining = YES;
request.allHTTPHeaderFields = wself.HTTPHeaders; request.allHTTPHeaderFields = wself.HTTPHeaders;
operation = [SDWebImageDownloaderOperation.alloc initWithRequest:request queue:wself.workingQueue options:options progress:^(NSUInteger receivedSize, long long expectedSize) operation = [SDWebImageDownloaderOperation.alloc initWithRequest:request options:options progress:^(NSUInteger receivedSize, long long expectedSize)
{ {
if (!wself) return; if (!wself) return;
SDWebImageDownloader *sself = wself; SDWebImageDownloader *sself = wself;

View File

@ -16,7 +16,6 @@
@property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options; @property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options;
- (id)initWithRequest:(NSURLRequest *)request - (id)initWithRequest:(NSURLRequest *)request
queue:(dispatch_queue_t)queue
options:(SDWebImageDownloaderOptions)options options:(SDWebImageDownloaderOptions)options
progress:(SDWebImageDownloaderProgressBlock)progressBlock progress:(SDWebImageDownloaderProgressBlock)progressBlock
completed:(SDWebImageDownloaderCompletedBlock)completedBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock

View File

@ -21,7 +21,6 @@
@property (assign, nonatomic) long long expectedSize; @property (assign, nonatomic) long long expectedSize;
@property (strong, nonatomic) NSMutableData *imageData; @property (strong, nonatomic) NSMutableData *imageData;
@property (strong, nonatomic) NSURLConnection *connection; @property (strong, nonatomic) NSURLConnection *connection;
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t queue;
@end @end
@ -31,11 +30,10 @@
BOOL responseFromCached; BOOL responseFromCached;
} }
- (id)initWithRequest:(NSURLRequest *)request queue:(dispatch_queue_t)queue options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock cancelled:(void (^)())cancelBlock - (id)initWithRequest:(NSURLRequest *)request options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock cancelled:(void (^)())cancelBlock
{ {
if ((self = [super init])) if ((self = [super init]))
{ {
_queue = queue;
_request = request; _request = request;
_options = options; _options = options;
_progressBlock = [progressBlock copy]; _progressBlock = [progressBlock copy];
@ -51,8 +49,6 @@
- (void)start - (void)start
{ {
dispatch_async(dispatch_get_main_queue(), ^
{
if (self.isCancelled) if (self.isCancelled)
{ {
self.finished = YES; self.finished = YES;
@ -63,12 +59,6 @@
self.executing = YES; self.executing = YES;
self.connection = [NSURLConnection.alloc initWithRequest:self.request delegate:self startImmediately:NO]; self.connection = [NSURLConnection.alloc initWithRequest:self.request delegate:self startImmediately:NO];
// If not in low priority mode, ensure we aren't blocked by UI manipulations (default runloop mode for NSURLConnection is NSEventTrackingRunLoopMode)
if (!(self.options & SDWebImageDownloaderLowPriority))
{
[self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
[self.connection start]; [self.connection start];
if (self.connection) if (self.connection)
@ -78,6 +68,9 @@
self.progressBlock(0, -1); self.progressBlock(0, -1);
} }
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
// Make sure to run the runloop in our background thread so it can process downloaded data
CFRunLoopRun();
} }
else else
{ {
@ -86,7 +79,6 @@
self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Connection can't be initialized"}], YES); self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Connection can't be initialized"}], YES);
} }
} }
});
} }
- (void)cancel - (void)cancel
@ -118,14 +110,11 @@
- (void)reset - (void)reset
{ {
dispatch_async(dispatch_get_main_queue(), ^
{
self.cancelBlock = nil; self.cancelBlock = nil;
self.completedBlock = nil; self.completedBlock = nil;
self.progressBlock = nil; self.progressBlock = nil;
self.connection = nil; self.connection = nil;
self.imageData = nil; self.imageData = nil;
});
} }
- (void)setFinished:(BOOL)finished - (void)setFinished:(BOOL)finished
@ -182,8 +171,6 @@
[self.imageData appendData:data]; [self.imageData appendData:data];
if ((self.options & SDWebImageDownloaderProgressiveDownload) && self.expectedSize > 0 && self.completedBlock) if ((self.options & SDWebImageDownloaderProgressiveDownload) && self.expectedSize > 0 && self.completedBlock)
{
dispatch_async(self.queue, ^
{ {
// The following code is from http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/ // The following code is from http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/
// Thanks to the author @Nyx0uf // Thanks to the author @Nyx0uf
@ -251,21 +238,19 @@
} }
CFRelease(imageSource); CFRelease(imageSource);
});
NSUInteger received = self.imageData.length; NSUInteger received = self.imageData.length;
dispatch_async(dispatch_get_main_queue(), ^
{
if (self.progressBlock) if (self.progressBlock)
{ {
self.progressBlock(received, self.expectedSize); self.progressBlock(received, self.expectedSize);
} }
});
} }
} }
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
{ {
CFRunLoopStop(CFRunLoopGetCurrent());
self.connection = nil; self.connection = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
@ -281,12 +266,8 @@
[self done]; [self done];
} }
else else
{
dispatch_async(self.queue, ^
{ {
UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)]; UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)];
dispatch_async(dispatch_get_main_queue(), ^
{
if (CGSizeEqualToSize(image.size, CGSizeZero)) if (CGSizeEqualToSize(image.size, CGSizeZero))
{ {
completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES); completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES);
@ -297,8 +278,6 @@
} }
self.completionBlock = nil; self.completionBlock = nil;
[self done]; [self done];
});
});
} }
} }
else else
@ -309,6 +288,7 @@
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{ {
CFRunLoopStop(CFRunLoopGetCurrent());
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
if (self.completedBlock) if (self.completedBlock)