Merge pull request #373 from cfis/download_cleanup
More SDWebImageDownloader Cleanup
This commit is contained in:
commit
4965c7d09f
|
@ -23,7 +23,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
|
|||
@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
|
||||
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t workingQueue;
|
||||
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t barrierQueue;
|
||||
|
||||
@end
|
||||
|
@ -72,7 +71,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
|
|||
_downloadQueue.maxConcurrentOperationCount = 2;
|
||||
_URLCallbacks = NSMutableDictionary.new;
|
||||
_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);
|
||||
}
|
||||
return self;
|
||||
|
@ -81,7 +79,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
|
|||
- (void)dealloc
|
||||
{
|
||||
[self.downloadQueue cancelAllOperations];
|
||||
SDDispatchQueueRelease(_workingQueue);
|
||||
SDDispatchQueueRelease(_barrierQueue);
|
||||
}
|
||||
|
||||
|
@ -124,7 +121,7 @@ static NSString *const kCompletedCallbackKey = @"completed";
|
|||
request.HTTPShouldHandleCookies = NO;
|
||||
request.HTTPShouldUsePipelining = YES;
|
||||
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;
|
||||
SDWebImageDownloader *sself = wself;
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
@property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options;
|
||||
|
||||
- (id)initWithRequest:(NSURLRequest *)request
|
||||
queue:(dispatch_queue_t)queue
|
||||
options:(SDWebImageDownloaderOptions)options
|
||||
progress:(SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(SDWebImageDownloaderCompletedBlock)completedBlock
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
@property (assign, nonatomic) long long expectedSize;
|
||||
@property (strong, nonatomic) NSMutableData *imageData;
|
||||
@property (strong, nonatomic) NSURLConnection *connection;
|
||||
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t queue;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -31,11 +30,10 @@
|
|||
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]))
|
||||
{
|
||||
_queue = queue;
|
||||
_request = request;
|
||||
_options = options;
|
||||
_progressBlock = [progressBlock copy];
|
||||
|
@ -51,8 +49,6 @@
|
|||
|
||||
- (void)start
|
||||
{
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
if (self.isCancelled)
|
||||
{
|
||||
self.finished = YES;
|
||||
|
@ -63,12 +59,6 @@
|
|||
self.executing = YES;
|
||||
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];
|
||||
|
||||
if (self.connection)
|
||||
|
@ -78,6 +68,9 @@
|
|||
self.progressBlock(0, -1);
|
||||
}
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
|
||||
|
||||
// Make sure to run the runloop in our background thread so it can process downloaded data
|
||||
CFRunLoopRun();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -86,7 +79,6 @@
|
|||
self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Connection can't be initialized"}], YES);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (void)cancel
|
||||
|
@ -118,14 +110,11 @@
|
|||
|
||||
- (void)reset
|
||||
{
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
self.cancelBlock = nil;
|
||||
self.completedBlock = nil;
|
||||
self.progressBlock = nil;
|
||||
self.connection = nil;
|
||||
self.imageData = nil;
|
||||
});
|
||||
}
|
||||
|
||||
- (void)setFinished:(BOOL)finished
|
||||
|
@ -182,8 +171,6 @@
|
|||
[self.imageData appendData:data];
|
||||
|
||||
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/
|
||||
// Thanks to the author @Nyx0uf
|
||||
|
@ -251,21 +238,19 @@
|
|||
}
|
||||
|
||||
CFRelease(imageSource);
|
||||
});
|
||||
|
||||
NSUInteger received = self.imageData.length;
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
|
||||
if (self.progressBlock)
|
||||
{
|
||||
self.progressBlock(received, self.expectedSize);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
|
||||
{
|
||||
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||
self.connection = nil;
|
||||
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
|
||||
|
@ -281,12 +266,8 @@
|
|||
[self done];
|
||||
}
|
||||
else
|
||||
{
|
||||
dispatch_async(self.queue, ^
|
||||
{
|
||||
UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)];
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
if (CGSizeEqualToSize(image.size, CGSizeZero))
|
||||
{
|
||||
completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES);
|
||||
|
@ -297,8 +278,6 @@
|
|||
}
|
||||
self.completionBlock = nil;
|
||||
[self done];
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -309,6 +288,7 @@
|
|||
|
||||
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
|
||||
{
|
||||
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
|
||||
|
||||
if (self.completedBlock)
|
||||
|
|
Loading…
Reference in New Issue