Add a convenience method to allow cancel on downloadToken

This commit is contained in:
DreamPiggy 2018-01-18 15:08:12 +08:00
parent 9c7224fd50
commit 3b4dd0b184
3 changed files with 28 additions and 1 deletions

View File

@ -82,9 +82,16 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
/** /**
* A token associated with each download. Can be used to cancel a download * A token associated with each download. Can be used to cancel a download
*/ */
@interface SDWebImageDownloadToken : NSObject @interface SDWebImageDownloadToken : NSObject <SDWebImageOperation>
/**
The download's URL. This should be readonly and you should not modify
*/
@property (nonatomic, strong, nullable) NSURL *url; @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; @property (nonatomic, strong, nullable) id downloadOperationCancelToken;
@end @end

View File

@ -9,7 +9,23 @@
#import "SDWebImageDownloader.h" #import "SDWebImageDownloader.h"
#import "SDWebImageDownloaderOperation.h" #import "SDWebImageDownloaderOperation.h"
@interface SDWebImageDownloadToken ()
@property (nonatomic, weak, nullable) NSOperation<SDWebImageDownloaderOperationInterface> *downloadOperation;
@end
@implementation SDWebImageDownloadToken @implementation SDWebImageDownloadToken
- (void)cancel {
if (self.downloadOperation) {
SDWebImageDownloadToken *cancelToken = self.downloadOperationCancelToken;
if (cancelToken) {
[self.downloadOperation cancel:cancelToken];
}
}
}
@end @end
@ -258,6 +274,7 @@
id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock]; id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock];
token = [SDWebImageDownloadToken new]; token = [SDWebImageDownloadToken new];
token.downloadOperation = operation;
token.url = url; token.url = url;
token.downloadOperationCancelToken = downloadOperationCancelToken; token.downloadOperationCancelToken = downloadOperationCancelToken;
}); });

View File

@ -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 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<NSObject> @protocol SDWebImageDownloaderOperationInterface<NSObject>
@ -35,6 +36,8 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
- (nullable NSURLCredential *)credential; - (nullable NSURLCredential *)credential;
- (void)setCredential:(nullable NSURLCredential *)value; - (void)setCredential:(nullable NSURLCredential *)value;
- (BOOL)cancel:(nullable id)token;
@end @end