From bb424d44fdd8a4d41c43c86387de3b32e1941cf8 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 30 Jan 2020 16:52:55 +0800 Subject: [PATCH] Added the URLSessionTaskMetrics support for downloader && operation, which can be used for network metrics --- SDWebImage/Core/SDWebImageDownloader.m | 9 +++++++++ SDWebImage/Core/SDWebImageDownloaderOperation.h | 7 +++++++ SDWebImage/Core/SDWebImageDownloaderOperation.m | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/SDWebImage/Core/SDWebImageDownloader.m b/SDWebImage/Core/SDWebImageDownloader.m index 94bfa049..c836cc59 100644 --- a/SDWebImage/Core/SDWebImageDownloader.m +++ b/SDWebImage/Core/SDWebImageDownloader.m @@ -498,6 +498,15 @@ didReceiveResponse:(NSURLResponse *)response } } +- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)) { + + // Identify the operation that runs this task and pass it the delegate method + NSOperation *dataOperation = [self operationWithTask:task]; + if ([dataOperation respondsToSelector:@selector(URLSession:task:didFinishCollectingMetrics:)]) { + [dataOperation URLSession:session task:task didFinishCollectingMetrics:metrics]; + } +} + @end @implementation SDWebImageDownloadToken diff --git a/SDWebImage/Core/SDWebImageDownloaderOperation.h b/SDWebImage/Core/SDWebImageDownloaderOperation.h index e987ba42..3b93aa71 100644 --- a/SDWebImage/Core/SDWebImageDownloaderOperation.h +++ b/SDWebImage/Core/SDWebImageDownloaderOperation.h @@ -36,6 +36,7 @@ @optional @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask; +@property (strong, nonatomic, readonly, nullable) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); @property (strong, nonatomic, nullable) NSURLCredential *credential; @property (assign, nonatomic) double minimumProgressInterval; @@ -62,6 +63,12 @@ */ @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask; +/** + * The collected metrics from `-URLSession:task:didFinishCollectingMetrics:`. + * This can be used to collect the network metrics like download duration, DNS lookup duration, SSL handshake dureation, etc. See Apple's documentation: https://developer.apple.com/documentation/foundation/urlsessiontaskmetrics + */ +@property (strong, nonatomic, readonly, nullable) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); + /** * The credential used for authentication challenges in `-URLSession:task:didReceiveChallenge:completionHandler:`. * diff --git a/SDWebImage/Core/SDWebImageDownloaderOperation.m b/SDWebImage/Core/SDWebImageDownloaderOperation.m index 6527eddd..355e207e 100644 --- a/SDWebImage/Core/SDWebImageDownloaderOperation.m +++ b/SDWebImage/Core/SDWebImageDownloaderOperation.m @@ -52,6 +52,8 @@ typedef NSMutableDictionary SDCallbacksDictionary; @property (strong, nonatomic, readwrite, nullable) NSURLSessionTask *dataTask; +@property (strong, nonatomic, readwrite, nullable) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); + @property (strong, nonatomic, nonnull) dispatch_queue_t coderQueue; // the queue to do image decoding #if SD_UIKIT @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundTaskId; @@ -512,6 +514,10 @@ didReceiveResponse:(NSURLResponse *)response } } +- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)) { + self.metrics = metrics; +} + #pragma mark Helper methods + (SDWebImageOptions)imageOptionsFromDownloaderOptions:(SDWebImageDownloaderOptions)downloadOptions { SDWebImageOptions options = 0;