Update the downloader token's property to match the comments

This commit is contained in:
DreamPiggy 2018-03-31 15:02:10 +08:00
parent c3892d7d08
commit 47aa73a436
4 changed files with 50 additions and 30 deletions

View File

@ -92,14 +92,14 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
@interface SDWebImageDownloadToken : NSObject <SDWebImageOperation>
/**
The download's URL. This should be readonly and you should not modify
Cancel the current download.
*/
@property (nonatomic, strong, nullable) NSURL *url;
- (void)cancel;
/**
The cancel token taken from `addHandlersForProgress:completed`. This should be readonly and you should not modify
@note use `-[SDWebImageDownloadToken cancel]` to cancel the token
The download's URL.
*/
@property (nonatomic, strong, nullable) id downloadOperationCancelToken;
@property (nonatomic, strong, nullable, readonly) NSURL *url;
@end
@ -139,7 +139,7 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
@property (nonatomic, assign, readonly) NSUInteger currentDownloadCount;
/**
* Returns the global shared downloader instance. Which use the `SDWebImageDownloaderConfig.defaultDownloaderConfiguration` config.
* Returns the global shared downloader instance. Which use the `SDWebImageDownloaderConfig.defaultDownloaderConfig` config.
*/
@property (nonatomic, class, readonly, nonnull) SDWebImageDownloader *sharedDownloader;

View File

@ -15,20 +15,10 @@
@interface SDWebImageDownloadToken ()
@property (nonatomic, strong, nullable, readwrite) NSURL *url;
@property (nonatomic, strong, nullable, readwrite) id downloadOperationCancelToken;
@property (nonatomic, weak, nullable) NSOperation<SDWebImageDownloaderOperation> *downloadOperation;
@end
@implementation SDWebImageDownloadToken
- (void)cancel {
if (self.downloadOperation) {
SDWebImageDownloadToken *cancelToken = self.downloadOperationCancelToken;
if (cancelToken) {
[self.downloadOperation cancel:cancelToken];
}
}
}
@property (nonatomic, weak, nullable) SDWebImageDownloader *downloader;
@end
@ -284,6 +274,7 @@
token.downloadOperation = operation;
token.url = url;
token.downloadOperationCancelToken = downloadOperationCancelToken;
token.downloader = self;
return token;
}
@ -398,3 +389,23 @@ didReceiveResponse:(NSURLResponse *)response
}
@end
@implementation SDWebImageDownloadToken
- (void)cancel {
@synchronized (self) {
if (!self.downloadOperationCancelToken) {
return;
}
if (self.downloader) {
// Downloader is alive, cancel token
[self.downloader cancel:self];
} else {
// Downloader is dealloced, only cancel download operation
[self.downloadOperation cancel:self.downloadOperationCancelToken];
}
self.downloadOperationCancelToken = nil;
}
}
@end

View File

@ -24,7 +24,8 @@ typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
@interface SDWebImageDownloaderConfig : NSObject <NSCopying>
/**
Gets/Sets the default downloader config.
Gets/Sets the default downloader config used for shared instance or initialization when it does not provide any downloader config. Such as `SDWebImageDownloader.sharedDownloader`.
@note You should not pass nil to this value.
*/
@property (nonatomic, class, nonnull) SDWebImageDownloaderConfig *defaultDownloaderConfig;
@ -35,46 +36,54 @@ typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
@property (nonatomic, assign) BOOL shouldDecompressImages;
/**
* The maximum number of concurrent downloads
* The maximum number of concurrent downloads.
* Defaults to 6.
*/
@property (nonatomic, assign) NSInteger maxConcurrentDownloads;
/**
* The timeout value (in seconds) for the download operation. Default: 15.0.
* The timeout value (in seconds) for the download operation.
* Defaults to 15.0.
*/
@property (nonatomic, assign) NSTimeInterval downloadTimeout;
/**
* The custom session configuration in use by NSURLSession.
* The custom session configuration in use by NSURLSession. If you don't provide one, we will use `defaultSessionConfiguration` instead.
* Defatuls to nil.
* @note The `timeoutIntervalForRequest` will be override by `downloadTimeout` config.
*/
@property (nonatomic, strong, nonnull) NSURLSessionConfiguration *sessionConfiguration;
@property (nonatomic, strong, nullable) NSURLSessionConfiguration *sessionConfiguration;
/**
* Gets/Sets a subclass of `SDWebImageDownloaderOperation` as the default
* `NSOperation` to be used each time SDWebImage constructs a request
* operation to download an image.
*
* Defaults to nil.
* @note Passing `NSOperation<SDWebImageDownloaderOperation>` to set as default. Passing `nil` will revert to `SDWebImageDownloaderOperation`.
*/
@property (nonatomic, assign, nullable) Class operationClass;
/**
* Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`.
* Changes download operations execution order.
* Defaults to `SDWebImageDownloaderFIFOExecutionOrder`.
*/
@property (nonatomic, assign) SDWebImageDownloaderExecutionOrder executionOrder;
/**
* Set the default URL credential to be set for request operations.
* Set the default URL credential to be set for request operations.
* Defaults to nil.
*/
@property (nonatomic, strong, nullable) NSURLCredential *urlCredential;
/**
* Set username
* Set username using for HTTP Basic authentication.
* Defaults to nil.
*/
@property (nonatomic, copy, nullable) NSString *username;
/**
* Set password
* Set password using for HTTP Basic authentication.
* Defautls to nil.
*/
@property (nonatomic, copy, nullable) NSString *password;

View File

@ -29,7 +29,7 @@ static SDWebImageDownloaderConfig * _defaultDownloaderConfig;
self = [super init];
if (self) {
_shouldDecompressImages = YES;
_maxConcurrentDownloads = 3;
_maxConcurrentDownloads = 6;
_downloadTimeout = 15.0;
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
}