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
- (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<SDWebImageDownloaderOperation> *)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<SDWebImageDownloaderOperation> *downloadOperation = notification.object;
if (downloadOperation && downloadOperation == self.downloadOperation) {
if (downloadOperation) {
self.response = downloadOperation.response;
}
}

View File

@ -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<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;