From ed894ecff59aa5acb3a11dfb21490f974a76066f Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 30 Jan 2020 17:32:46 +0800 Subject: [PATCH] Added the metrics in the download token, make it easy to grab the information right in completion block (is this useful ?) --- SDWebImage/Core/SDWebImageDownloader.h | 5 +++++ SDWebImage/Core/SDWebImageDownloader.m | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/SDWebImage/Core/SDWebImageDownloader.h b/SDWebImage/Core/SDWebImageDownloader.h index 571b72a2..a365395c 100644 --- a/SDWebImage/Core/SDWebImageDownloader.h +++ b/SDWebImage/Core/SDWebImageDownloader.h @@ -128,6 +128,11 @@ typedef SDImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock; */ @property (nonatomic, strong, nullable, readonly) NSURLResponse *response; +/** + The download's metrics. This will be nil if download operation does not support metrics. + */ +@property (nonatomic, strong, nullable, readonly) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); + @end diff --git a/SDWebImage/Core/SDWebImageDownloader.m b/SDWebImage/Core/SDWebImageDownloader.m index c836cc59..d7deee95 100644 --- a/SDWebImage/Core/SDWebImageDownloader.m +++ b/SDWebImage/Core/SDWebImageDownloader.m @@ -24,6 +24,7 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext; @property (nonatomic, strong, nullable, readwrite) NSURL *url; @property (nonatomic, strong, nullable, readwrite) NSURLRequest *request; @property (nonatomic, strong, nullable, readwrite) NSURLResponse *response; +@property (nonatomic, strong, nullable, readwrite) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); @property (nonatomic, weak, nullable, readwrite) id downloadOperationCancelToken; @property (nonatomic, weak, nullable) NSOperation *downloadOperation; @property (nonatomic, assign, getter=isCancelled) BOOL cancelled; @@ -519,18 +520,30 @@ didReceiveResponse:(NSURLResponse *)response self = [super init]; if (self) { _downloadOperation = downloadOperation; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:downloadOperation]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDidReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:downloadOperation]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDidStop:) name:SDWebImageDownloadStopNotification object:downloadOperation]; } return self; } -- (void)downloadReceiveResponse:(NSNotification *)notification { +- (void)downloadDidReceiveResponse:(NSNotification *)notification { NSOperation *downloadOperation = notification.object; if (downloadOperation && downloadOperation == self.downloadOperation) { self.response = downloadOperation.response; } } +- (void)downloadDidStop:(NSNotification *)notification { + NSOperation *downloadOperation = notification.object; + if (downloadOperation && downloadOperation == self.downloadOperation) { + if ([downloadOperation respondsToSelector:@selector(metrics)]) { + if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, watchOS 3.0, *)) { + self.metrics = downloadOperation.metrics; + } + } + } +} + - (void)cancel { @synchronized (self) { if (self.isCancelled) {