diff --git a/SDWebImage/SDWebImageDownloader.h b/SDWebImage/SDWebImageDownloader.h index 77a0ee8d..54224c5c 100644 --- a/SDWebImage/SDWebImageDownloader.h +++ b/SDWebImage/SDWebImageDownloader.h @@ -82,9 +82,16 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB /** * A token associated with each download. Can be used to cancel a download */ -@interface SDWebImageDownloadToken : NSObject +@interface SDWebImageDownloadToken : NSObject +/** + The download's URL. This should be readonly and you should not modify + */ @property (nonatomic, strong, nullable) NSURL *url; +/** + The cancel token taken from `addHandlersForProgress:completed`. This should be readonly and you should not modify + @note use `-[SDWebImageDownloadToken cancel]` to cancel the token + */ @property (nonatomic, strong, nullable) id downloadOperationCancelToken; @end diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 3adc937c..ca08ef77 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -9,7 +9,23 @@ #import "SDWebImageDownloader.h" #import "SDWebImageDownloaderOperation.h" +@interface SDWebImageDownloadToken () + +@property (nonatomic, weak, nullable) NSOperation *downloadOperation; + +@end + @implementation SDWebImageDownloadToken + +- (void)cancel { + if (self.downloadOperation) { + SDWebImageDownloadToken *cancelToken = self.downloadOperationCancelToken; + if (cancelToken) { + [self.downloadOperation cancel:cancelToken]; + } + } +} + @end @@ -258,6 +274,7 @@ id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock]; token = [SDWebImageDownloadToken new]; + token.downloadOperation = operation; token.url = url; token.downloadOperationCancelToken = downloadOperationCancelToken; }); diff --git a/SDWebImage/SDWebImageDownloaderOperation.h b/SDWebImage/SDWebImageDownloaderOperation.h index 40721b24..bb2dc7e3 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.h +++ b/SDWebImage/SDWebImageDownloaderOperation.h @@ -19,6 +19,7 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification /** Describes a downloader operation. If one wants to use a custom downloader op, it needs to inherit from `NSOperation` and conform to this protocol + For the description about these methods, see `SDWebImageDownloaderOperation` */ @protocol SDWebImageDownloaderOperationInterface @@ -35,6 +36,8 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification - (nullable NSURLCredential *)credential; - (void)setCredential:(nullable NSURLCredential *)value; +- (BOOL)cancel:(nullable id)token; + @end