Update the Example and Test case about URLSessionMetrics, expose the API in UIVIew+WebCache to make it easy to write code (or user have to write NSStringFromClass)

This commit is contained in:
DreamPiggy 2020-01-30 18:33:16 +08:00
parent ed894ecff5
commit d56636e15b
3 changed files with 54 additions and 4 deletions

View File

@ -113,10 +113,22 @@
}
cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
[cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]]
placeholderImage:placeholderImage
options:indexPath.row == 0 ? SDWebImageRefreshCached : 0
context:@{SDWebImageContextImageThumbnailPixelSize : @(CGSizeMake(180, 120))}];
__weak SDAnimatedImageView *imageView = cell.customImageView;
[imageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]]
placeholderImage:placeholderImage
options:indexPath.row == 0 ? SDWebImageRefreshCached : 0
context:@{SDWebImageContextImageThumbnailPixelSize : @(CGSizeMake(180, 120))}
progress:nil
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
SDWebImageCombinedOperation *operation = [imageView sd_imageLoadOperationForKey:imageView.sd_latestOperationKey];
SDWebImageDownloadToken *token = operation.loaderOperation;
if (@available(iOS 10.0, *)) {
NSURLSessionTaskMetrics *metrics = token.metrics;
if (metrics) {
printf("Metrics: %s download in (%f) seconds\n", [imageURL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding], metrics.taskInterval.duration);
}
}
}];
return cell;
}

View File

@ -31,6 +31,13 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
*/
@property (nonatomic, strong, readonly, nullable) NSURL *sd_imageURL;
/**
* Get the current image operation key. Operation key is used to identify the different queries for one view instance (like UIButton).
* See more about this in `SDWebImageContextSetImageOperationKey`.
* @note You can use method `UIView+WebCacheOperation` to invesigate different queries' operation.
*/
@property (nonatomic, strong, readonly, nullable) NSString *sd_latestOperationKey;
/**
* The current image loading progress associated to the view. The unit count is the received size and excepted size of download.
* The `totalUnitCount` and `completedUnitCount` will be reset to 0 after a new image loading start (change from current queue). And they will be set to `SDWebImageProgressUnitCountUnknown` if the progressBlock not been called but the image loading success to mark the progress finished (change from main queue).

View File

@ -642,6 +642,37 @@
}];
}
- (void)test26DownloadURLSessionMetrics {
XCTestExpectation *expectation1 = [self expectationWithDescription:@"Download URLSessionMetrics works"];
SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init];
__block SDWebImageDownloadToken *token;
token = [downloader downloadImageWithURL:[NSURL URLWithString:kTestJPEGURL] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).beNil();
if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, *)) {
NSURLSessionTaskMetrics *metrics = token.metrics;
expect(metrics).notTo.beNil();
expect(metrics.redirectCount).equal(0);
expect(metrics.transactionMetrics.count).equal(1);
NSURLSessionTaskTransactionMetrics *metric = metrics.transactionMetrics.firstObject;
// Metrcis Test
expect(metric.fetchStartDate).notTo.beNil();
expect(metric.connectStartDate).notTo.beNil();
expect(metric.connectEndDate).notTo.beNil();
expect(metric.networkProtocolName).equal(@"http/1.1");
expect(metric.resourceFetchType).equal(NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad);
expect(metric.isProxyConnection).beFalsy();
expect(metric.isReusedConnection).beFalsy();
}
[expectation1 fulfill];
}];
[self waitForExpectationsWithCommonTimeoutUsingHandler:^(NSError * _Nullable error) {
[downloader invalidateSessionAndCancel:YES];
}];
}
#pragma mark - SDWebImageLoader
- (void)test30CustomImageLoaderWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];