From 8cfda3dce91416988b028e50f142020b599ac688 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 8 Aug 2018 15:36:57 +0800 Subject: [PATCH] Make download receive response notification only dispatch to specific observer --- SDWebImage/SDWebImageDownloader.m | 14 +++++++------- SDWebImage/UIView+WebCacheOperation.m | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 4dc2e548..d10184d9 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -429,20 +429,20 @@ didReceiveResponse:(NSURLResponse *)response @implementation SDWebImageDownloadToken - (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:_downloadOperation]; } -- (instancetype)init { - self = [super init]; - if (self) { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:nil]; +- (void)setDownloadOperation:(NSOperation *)downloadOperation { + if (downloadOperation != _downloadOperation) { + [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:_downloadOperation]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:downloadOperation]; + _downloadOperation = downloadOperation; } - return self; } - (void)downloadReceiveResponse:(NSNotification *)notification { NSOperation *downloadOperation = notification.object; - if (downloadOperation && downloadOperation == self.downloadOperation) { + if (downloadOperation) { self.response = downloadOperation.response; } } diff --git a/SDWebImage/UIView+WebCacheOperation.m b/SDWebImage/UIView+WebCacheOperation.m index c3bfc1e8..83cec8aa 100644 --- a/SDWebImage/UIView+WebCacheOperation.m +++ b/SDWebImage/UIView+WebCacheOperation.m @@ -11,7 +11,7 @@ static char loadOperationKey; -// key is copy, value is weak because operation instance is retained by SDWebImageManager's runningOperations property +// key is strong, value is weak because operation instance is retained by SDWebImageManager's runningOperations property // we should use lock to keep thread-safe because these method may not be acessed from main queue typedef NSMapTable> SDOperationsDictionary;