Authored by DreamPiggy

Add a convenience method to allow cancel on downloadToken

@@ -82,9 +82,16 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB @@ -82,9 +82,16 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
82 /** 82 /**
83 * A token associated with each download. Can be used to cancel a download 83 * A token associated with each download. Can be used to cancel a download
84 */ 84 */
85 -@interface SDWebImageDownloadToken : NSObject 85 +@interface SDWebImageDownloadToken : NSObject <SDWebImageOperation>
86 86
  87 +/**
  88 + The download's URL. This should be readonly and you should not modify
  89 + */
87 @property (nonatomic, strong, nullable) NSURL *url; 90 @property (nonatomic, strong, nullable) NSURL *url;
  91 +/**
  92 + The cancel token taken from `addHandlersForProgress:completed`. This should be readonly and you should not modify
  93 + @note use `-[SDWebImageDownloadToken cancel]` to cancel the token
  94 + */
88 @property (nonatomic, strong, nullable) id downloadOperationCancelToken; 95 @property (nonatomic, strong, nullable) id downloadOperationCancelToken;
89 96
90 @end 97 @end
@@ -9,7 +9,23 @@ @@ -9,7 +9,23 @@
9 #import "SDWebImageDownloader.h" 9 #import "SDWebImageDownloader.h"
10 #import "SDWebImageDownloaderOperation.h" 10 #import "SDWebImageDownloaderOperation.h"
11 11
  12 +@interface SDWebImageDownloadToken ()
  13 +
  14 +@property (nonatomic, weak, nullable) NSOperation<SDWebImageDownloaderOperationInterface> *downloadOperation;
  15 +
  16 +@end
  17 +
12 @implementation SDWebImageDownloadToken 18 @implementation SDWebImageDownloadToken
  19 +
  20 +- (void)cancel {
  21 + if (self.downloadOperation) {
  22 + SDWebImageDownloadToken *cancelToken = self.downloadOperationCancelToken;
  23 + if (cancelToken) {
  24 + [self.downloadOperation cancel:cancelToken];
  25 + }
  26 + }
  27 +}
  28 +
13 @end 29 @end
14 30
15 31
@@ -258,6 +274,7 @@ @@ -258,6 +274,7 @@
258 id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock]; 274 id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock];
259 275
260 token = [SDWebImageDownloadToken new]; 276 token = [SDWebImageDownloadToken new];
  277 + token.downloadOperation = operation;
261 token.url = url; 278 token.url = url;
262 token.downloadOperationCancelToken = downloadOperationCancelToken; 279 token.downloadOperationCancelToken = downloadOperationCancelToken;
263 }); 280 });
@@ -19,6 +19,7 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification @@ -19,6 +19,7 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
19 19
20 /** 20 /**
21 Describes a downloader operation. If one wants to use a custom downloader op, it needs to inherit from `NSOperation` and conform to this protocol 21 Describes a downloader operation. If one wants to use a custom downloader op, it needs to inherit from `NSOperation` and conform to this protocol
  22 + For the description about these methods, see `SDWebImageDownloaderOperation`
22 */ 23 */
23 @protocol SDWebImageDownloaderOperationInterface<NSObject> 24 @protocol SDWebImageDownloaderOperationInterface<NSObject>
24 25
@@ -35,6 +36,8 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification @@ -35,6 +36,8 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
35 - (nullable NSURLCredential *)credential; 36 - (nullable NSURLCredential *)credential;
36 - (void)setCredential:(nullable NSURLCredential *)value; 37 - (void)setCredential:(nullable NSURLCredential *)value;
37 38
  39 +- (BOOL)cancel:(nullable id)token;
  40 +
38 @end 41 @end
39 42
40 43