From c547adc2a9887e32657c25721141c1b348f6d507 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 7 Jan 2019 17:05:03 +0800 Subject: [PATCH 1/2] Retain operation when post download related notifications --- SDWebImage/SDWebImageDownloaderOperation.m | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 34d701b0..82a567e2 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -193,9 +193,9 @@ typedef NSMutableDictionary SDCallbacksDictionary; for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) { progressBlock(0, NSURLResponseUnknownLength, self.request.URL); } - __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:weakSelf]; + __strong typeof(self) strongSelf = self; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:strongSelf]; }); } else { [self callCompletionBlocksWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorUnknown userInfo:@{NSLocalizedDescriptionKey : @"Task can't be initialized"}]]; @@ -216,9 +216,9 @@ typedef NSMutableDictionary SDCallbacksDictionary; if (self.dataTask) { [self.dataTask cancel]; - __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:weakSelf]; + __strong typeof(self) strongSelf = self; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:strongSelf]; }); // As we cancelled the task, its callback won't be called and thus won't @@ -304,9 +304,9 @@ didReceiveResponse:(NSURLResponse *)response disposition = NSURLSessionResponseCancel; } - __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:weakSelf]; + __strong typeof(self) strongSelf = self; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:strongSelf]; }); if (completionHandler) { @@ -384,11 +384,11 @@ didReceiveResponse:(NSURLResponse *)response - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { @synchronized(self) { self.dataTask = nil; - __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:weakSelf]; + __strong typeof(self) strongSelf = self; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:strongSelf]; if (!error) { - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:weakSelf]; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:strongSelf]; } }); } From 3a470fc8b0f64659cb7b2dede449273489bcdec6 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 11 Jan 2019 15:11:01 +0800 Subject: [PATCH 2/2] Using __block instead --- SDWebImage/SDWebImageDownloaderOperation.m | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 82a567e2..8cb4ba22 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -193,8 +193,8 @@ typedef NSMutableDictionary SDCallbacksDictionary; for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) { progressBlock(0, NSURLResponseUnknownLength, self.request.URL); } + __block typeof(self) strongSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(self) strongSelf = self; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:strongSelf]; }); } else { @@ -216,8 +216,8 @@ typedef NSMutableDictionary SDCallbacksDictionary; if (self.dataTask) { [self.dataTask cancel]; + __block typeof(self) strongSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(self) strongSelf = self; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:strongSelf]; }); @@ -303,9 +303,8 @@ didReceiveResponse:(NSURLResponse *)response // Status code invalid and marked as cancelled. Do not call `[self.dataTask cancel]` which may mass up URLSession life cycle disposition = NSURLSessionResponseCancel; } - + __block typeof(self) strongSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(self) strongSelf = self; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:strongSelf]; }); @@ -384,8 +383,8 @@ didReceiveResponse:(NSURLResponse *)response - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { @synchronized(self) { self.dataTask = nil; + __block typeof(self) strongSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(self) strongSelf = self; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:strongSelf]; if (!error) { [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:strongSelf];