Remove setter of downloadOperation

This commit is contained in:
zhongwuzw 2018-08-09 14:59:04 +08:00
parent 8cfda3dce9
commit ab5b5677b2
2 changed files with 19 additions and 8 deletions

View File

@ -91,6 +91,8 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStopNotification;
typedef SDImageLoaderProgressBlock SDWebImageDownloaderProgressBlock;
typedef SDImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock;
@protocol SDWebImageDownloaderOperation;
/**
* A token associated with each download. Can be used to cancel a download
*/
@ -116,6 +118,11 @@ typedef SDImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock;
*/
@property (nonatomic, strong, nullable, readonly) NSURLResponse *response;
/**
Init download token with downloadOperation.
*/
- (nonnull instancetype)initWithDownloadOperation:(nullable NSOperation<SDWebImageDownloaderOperation> *)downloadOperation;
@end

View File

@ -199,8 +199,7 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock];
SDWebImageDownloadToken *token = [SDWebImageDownloadToken new];
token.downloadOperation = operation;
SDWebImageDownloadToken *token = [[SDWebImageDownloadToken alloc] initWithDownloadOperation:operation];
token.url = url;
token.request = operation.request;
token.downloadOperationCancelToken = downloadOperationCancelToken;
@ -429,20 +428,25 @@ didReceiveResponse:(NSURLResponse *)response
@implementation SDWebImageDownloadToken
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:_downloadOperation];
[[NSNotificationCenter defaultCenter] removeObserver:self 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];
- (instancetype)init {
return [self initWithDownloadOperation:nil];
}
- (instancetype)initWithDownloadOperation:(NSOperation<SDWebImageDownloaderOperation> *)downloadOperation {
self = [super init];
if (self) {
_downloadOperation = downloadOperation;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:downloadOperation];
}
return self;
}
- (void)downloadReceiveResponse:(NSNotification *)notification {
NSOperation<SDWebImageDownloaderOperation> *downloadOperation = notification.object;
if (downloadOperation) {
if (downloadOperation && downloadOperation == self.downloadOperation) {
self.response = downloadOperation.response;
}
}