From b85556fa86e8a8d14ead4225646959ee6b0d86da Mon Sep 17 00:00:00 2001 From: Charlie Savage Date: Fri, 26 Apr 2013 16:30:00 -0600 Subject: [PATCH] This is an attempt to avoid the crashes in #341. It won't fix the underlying issue but I hope it will avoid it in most cases. The various crash reports indicate the underlying download operation is being freed before the async block in dataReceived is being executed. This fix change tries to avoid every calling the async block. --- SDWebImage/SDWebImageDownloaderOperation.m | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 07b3ad53..651bc765 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -160,10 +160,7 @@ self.progressBlock(0, expected); } - dispatch_async(self.queue, ^ - { - self.imageData = [NSMutableData.alloc initWithCapacity:expected]; - }); + self.imageData = [NSMutableData.alloc initWithCapacity:expected]; } else { @@ -182,11 +179,11 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { - dispatch_async(self.queue, ^ - { - [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/ // Thanks to the author @Nyx0uf @@ -254,7 +251,8 @@ } CFRelease(imageSource); - } + }); + NSUInteger received = self.imageData.length; dispatch_async(dispatch_get_main_queue(), ^ { @@ -263,7 +261,7 @@ self.progressBlock(received, self.expectedSize); } }); - }); + } } - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection