Make download receive response notification only dispatch to specific observer

This commit is contained in:
zhongwuzw 2018-08-08 15:36:57 +08:00
parent 3b1219cdfd
commit 8cfda3dce9
2 changed files with 8 additions and 8 deletions

View File

@ -429,20 +429,20 @@ didReceiveResponse:(NSURLResponse *)response
@implementation SDWebImageDownloadToken @implementation SDWebImageDownloadToken
- (void)dealloc { - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:_downloadOperation];
} }
- (instancetype)init { - (void)setDownloadOperation:(NSOperation<SDWebImageDownloaderOperation> *)downloadOperation {
self = [super init]; if (downloadOperation != _downloadOperation) {
if (self) { [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:_downloadOperation];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:downloadOperation];
_downloadOperation = downloadOperation;
} }
return self;
} }
- (void)downloadReceiveResponse:(NSNotification *)notification { - (void)downloadReceiveResponse:(NSNotification *)notification {
NSOperation<SDWebImageDownloaderOperation> *downloadOperation = notification.object; NSOperation<SDWebImageDownloaderOperation> *downloadOperation = notification.object;
if (downloadOperation && downloadOperation == self.downloadOperation) { if (downloadOperation) {
self.response = downloadOperation.response; self.response = downloadOperation.response;
} }
} }

View File

@ -11,7 +11,7 @@
static char loadOperationKey; 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 // we should use lock to keep thread-safe because these method may not be acessed from main queue
typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary; typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;