Resolved some downloader threading issues.
- callbacks could be released between the callback existence if check and invocation - could be nilled out in a background thread call to done, while being prepared for invocation on the main thread - now making sure done is always performed on the main thread - also added some related threading fixes and optimizations
This commit is contained in:
parent
331053d26e
commit
713a83381f
|
@ -49,6 +49,8 @@
|
|||
|
||||
- (void)start
|
||||
{
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
if (self.isCancelled)
|
||||
{
|
||||
self.finished = YES;
|
||||
|
@ -56,8 +58,6 @@
|
|||
return;
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
self.executing = YES;
|
||||
self.connection = [NSURLConnection.alloc initWithRequest:self.request delegate:self startImmediately:NO];
|
||||
|
||||
|
@ -143,20 +143,17 @@
|
|||
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
|
||||
{
|
||||
if (![response respondsToSelector:@selector(statusCode)] || [((NSHTTPURLResponse *)response) statusCode] < 400)
|
||||
{
|
||||
dispatch_async(self.queue, ^
|
||||
{
|
||||
NSUInteger expected = response.expectedContentLength > 0 ? (NSUInteger)response.expectedContentLength : 0;
|
||||
self.imageData = [NSMutableData.alloc initWithCapacity:expected];
|
||||
self.expectedSize = expected;
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
if (self.progressBlock)
|
||||
{
|
||||
self.progressBlock(0, expected);
|
||||
}
|
||||
});
|
||||
|
||||
dispatch_async(self.queue, ^
|
||||
{
|
||||
self.imageData = [NSMutableData.alloc initWithCapacity:expected];
|
||||
});
|
||||
}
|
||||
else
|
||||
|
@ -249,11 +246,12 @@
|
|||
|
||||
CFRelease(imageSource);
|
||||
}
|
||||
NSUInteger received = self.imageData.length;
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
if (self.progressBlock)
|
||||
{
|
||||
self.progressBlock(self.imageData.length, self.expectedSize);
|
||||
self.progressBlock(received, self.expectedSize);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -263,23 +261,26 @@
|
|||
{
|
||||
self.connection = nil;
|
||||
|
||||
dispatch_async(self.queue, ^
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
|
||||
|
||||
if (self.completedBlock)
|
||||
SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
|
||||
if (completionBlock)
|
||||
{
|
||||
dispatch_async(self.queue, ^
|
||||
{
|
||||
__block SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
|
||||
UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)];
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
completionBlock(image, self.imageData, nil, YES);
|
||||
completionBlock = nil;
|
||||
});
|
||||
}
|
||||
|
||||
self.completionBlock = nil;
|
||||
[self done];
|
||||
});
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
[self done];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
|
||||
|
|
Loading…
Reference in New Issue