From ad7b31f4f9cf0c041f638cbbffe5d6d052518ed6 Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Thu, 28 Jun 2018 13:10:43 -0700 Subject: [PATCH 1/9] First stab at adding a block to be called and implementing didFinishCollectingMetrics on the SessionManager --- AFNetworking/AFURLSessionManager.h | 7 +++++++ AFNetworking/AFURLSessionManager.m | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/AFNetworking/AFURLSessionManager.h b/AFNetworking/AFURLSessionManager.h index e7ae0d8e1f..72adc12a98 100644 --- a/AFNetworking/AFURLSessionManager.h +++ b/AFNetworking/AFURLSessionManager.h @@ -378,6 +378,13 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)setTaskDidCompleteBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, NSError * _Nullable error))block; +/** + Sets a block to be executed when metrics are finalized related to a specific task, as handled by the `NSURLSessionTaskDelegate` method `URLSession:task:didFinishCollectingMetrics:`. + + @param block A block object to be executed when a session task is completed. The block has no return value, and takes three arguments: the session, the task, and any metrics that were collected in the process of executing the task. + */ +- (void)setTaskDidFinishCollectingMetricsBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, NSURLSessionTaskMetrics * _Nullable metrics))block; + ///------------------------------------------- /// @name Setting Data Task Delegate Callbacks ///------------------------------------------- diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 7a0529b700..35147b894b 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -97,6 +97,7 @@ static dispatch_group_t url_session_manager_completion_group() { typedef NSInputStream * (^AFURLSessionTaskNeedNewBodyStreamBlock)(NSURLSession *session, NSURLSessionTask *task); typedef void (^AFURLSessionTaskDidSendBodyDataBlock)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend); typedef void (^AFURLSessionTaskDidCompleteBlock)(NSURLSession *session, NSURLSessionTask *task, NSError *error); +typedef void (^AFURLSessionTaskDidFinishCollectingMetricsBlock)(NSURLSession *session, NSURLSessionTask *task, NSURLSessionTaskMetrics * metrics); typedef NSURLSessionResponseDisposition (^AFURLSessionDataTaskDidReceiveResponseBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response); typedef void (^AFURLSessionDataTaskDidBecomeDownloadTaskBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask); @@ -462,6 +463,7 @@ @interface AFURLSessionManager () @property (readwrite, nonatomic, copy) AFURLSessionTaskNeedNewBodyStreamBlock taskNeedNewBodyStream; @property (readwrite, nonatomic, copy) AFURLSessionTaskDidSendBodyDataBlock taskDidSendBodyData; @property (readwrite, nonatomic, copy) AFURLSessionTaskDidCompleteBlock taskDidComplete; +@property (readwrite, nonatomic, copy) AFURLSessionTaskDidFinishCollectingMetricsBlock taskDidFinishCollectingMetrics; @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidReceiveResponseBlock dataTaskDidReceiveResponse; @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidBecomeDownloadTaskBlock dataTaskDidBecomeDownloadTask; @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidReceiveDataBlock dataTaskDidReceiveData; @@ -871,6 +873,10 @@ - (void)setTaskDidCompleteBlock:(void (^)(NSURLSession *session, NSURLSessionTas self.taskDidComplete = block; } +- (void)setTaskDidFinishCollectingMetricsBlock:(void (^)(NSURLSession * _Nonnull, NSURLSessionTask * _Nonnull, NSURLSessionTaskMetrics * _Nullable))block { + self.taskDidFinishCollectingMetrics = block; +} + #pragma mark - - (void)setDataTaskDidReceiveResponseBlock:(NSURLSessionResponseDisposition (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response))block { @@ -1077,6 +1083,22 @@ - (void)URLSession:(NSURLSession *)session } } +- (void)URLSession:(NSURLSession *)session + task:(NSURLSessionTask *)task +didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics +{ + AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task]; + + // delegate may be nil when completing a task in the background + if (delegate && [delegate respondsToSelector:@selector(URLSession:task:didFinishCollectingMetrics:)]) { + [delegate URLSession:session task:task didFinishCollectingMetrics:metrics]; + } + + if (self.taskDidFinishCollectingMetrics) { + self.taskDidFinishCollectingMetrics(session, task, metrics); + } +} + #pragma mark - NSURLSessionDataDelegate - (void)URLSession:(NSURLSession *)session From fba4f366d20b6b9a42263a64aabbcf03916b6f1c Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Thu, 28 Jun 2018 16:15:18 -0700 Subject: [PATCH 2/9] Added kvo use of sessionTaskMetrics --- AFNetworking/AFURLSessionManager.h | 5 +++++ AFNetworking/AFURLSessionManager.m | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/AFNetworking/AFURLSessionManager.h b/AFNetworking/AFURLSessionManager.h index 72adc12a98..99e167a54b 100644 --- a/AFNetworking/AFURLSessionManager.h +++ b/AFNetworking/AFURLSessionManager.h @@ -505,4 +505,9 @@ FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteAssetPathKey; */ FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteErrorKey; +/** + The session task metrics taken from the download task. Included in the userInfo dictionary of the `AFNetworkingTaskDidCompleteSessionTaskMetrics` + */ +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteSessionTaskMetrics; + NS_ASSUME_NONNULL_END diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 35147b894b..cd0fdf7f97 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -82,6 +82,7 @@ static dispatch_group_t url_session_manager_completion_group() { NSString * const AFNetworkingTaskDidCompleteResponseDataKey = @"com.alamofire.networking.complete.finish.responsedata"; NSString * const AFNetworkingTaskDidCompleteErrorKey = @"com.alamofire.networking.task.complete.error"; NSString * const AFNetworkingTaskDidCompleteAssetPathKey = @"com.alamofire.networking.task.complete.assetpath"; +NSString * const AFNetworkingTaskDidCompleteSessionTaskMetrics = @"com.alamofire.networking.complete.sessiontaskmetrics"; static NSString * const AFURLSessionManagerLockName = @"com.alamofire.networking.session.manager.lock"; @@ -121,6 +122,7 @@ - (instancetype)initWithTask:(NSURLSessionTask *)task; @property (nonatomic, strong) NSProgress *uploadProgress; @property (nonatomic, strong) NSProgress *downloadProgress; @property (nonatomic, copy) NSURL *downloadFileURL; +@property (nonatomic, strong) NSURLSessionTaskMetrics *sessionTaskMetrics; @property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading; @property (nonatomic, copy) AFURLSessionTaskProgressBlock uploadProgressBlock; @property (nonatomic, copy) AFURLSessionTaskProgressBlock downloadProgressBlock; @@ -211,6 +213,14 @@ - (void)URLSession:(__unused NSURLSession *)session self.mutableData = nil; } +#if AF_CAN_USE_AT_AVAILABLE + if (@available(iOS 10, macOS 10.12, watchOS 3, tvOS 10, *)) { + if (self.sessionTaskMetrics) { + userInfo[AFNetworkingTaskDidCompleteSessionTaskMetrics] = self.sessionTaskMetrics; + } + } +#endif + if (self.downloadFileURL) { userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL; } else if (data) { @@ -259,6 +269,12 @@ - (void)URLSession:(__unused NSURLSession *)session } } +- (void)URLSession:(NSURLSession *)session + task:(NSURLSessionTask *)task +didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics { + self.sessionTaskMetrics = metrics; +} + #pragma mark - NSURLSessionDataDelegate - (void)URLSession:(__unused NSURLSession *)session @@ -1088,9 +1104,8 @@ - (void)URLSession:(NSURLSession *)session didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics { AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task]; - - // delegate may be nil when completing a task in the background - if (delegate && [delegate respondsToSelector:@selector(URLSession:task:didFinishCollectingMetrics:)]) { + // Metrics may fire after URLSession:task:didCompleteWithError: is called, delegate may be nil + if (delegate) { [delegate URLSession:session task:task didFinishCollectingMetrics:metrics]; } From aed00d309965f9b2db1d02492145afc03faa8cb5 Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Thu, 28 Jun 2018 16:38:16 -0700 Subject: [PATCH 3/9] Test for notification publishing the metrics --- Tests/Tests/AFURLSessionManagerTests.m | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index e4f2a6b199..c43e673b9c 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -133,6 +133,23 @@ - (void)testDownloadTaskDoesReportProgress { [self waitForExpectationsWithCommonTimeout]; } +- (void)testSessionTaskDoesReportMetrics { + [self expectationForNotification:AFNetworkingTaskDidCompleteNotification object:nil handler:^BOOL(NSNotification * _Nonnull notification) { +#if AF_CAN_USE_AT_AVAILABLE + if (@available(iOS 10, macOS 10.12, watchOS 3, tvOS 10, *)) { + return [notification userInfo][AFNetworkingTaskDidCompleteSessionTaskMetrics] != nil; + } +#endif + return YES; + }]; + NSURLSessionTask *task = [self.localManager downloadTaskWithRequest:[self bigImageURLRequest] + progress:nil + destination:nil + completionHandler:nil]; + [task resume]; + [self waitForExpectationsWithCommonTimeout]; +} + // iOS 7 has a bug that may return nil for a session. To simulate that, nil out the // session and it will return nil itself. - (void)testFileUploadTaskReturnsNilWithBug { From c4caddc5e98b0fbab5937d97c75977dee9035e51 Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Fri, 29 Jun 2018 10:50:58 -0700 Subject: [PATCH 4/9] Tests for notification and block use --- Tests/Tests/AFURLSessionManagerTests.m | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index c43e673b9c..deb067dfe2 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -142,6 +142,12 @@ - (void)testSessionTaskDoesReportMetrics { #endif return YES; }]; + + __weak XCTestExpectation *metricsBlock = [self expectationWithDescription:@"Metrics completion block is called"]; + [self.localManager setTaskDidFinishCollectingMetricsBlock:^(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLSessionTaskMetrics * _Nullable metrics) { + [metricsBlock fulfill]; + }]; + NSURLSessionTask *task = [self.localManager downloadTaskWithRequest:[self bigImageURLRequest] progress:nil destination:nil From 63410f3567213d6e4e2fa621d38e9ad8152c6d7b Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Fri, 29 Jun 2018 11:49:55 -0700 Subject: [PATCH 5/9] Xcode 7.x compatibility --- AFNetworking/AFURLSessionManager.h | 3 ++- AFNetworking/AFURLSessionManager.m | 12 +++++++++++- Tests/Tests/AFURLSessionManagerTests.m | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/AFNetworking/AFURLSessionManager.h b/AFNetworking/AFURLSessionManager.h index 99e167a54b..816c02d9aa 100644 --- a/AFNetworking/AFURLSessionManager.h +++ b/AFNetworking/AFURLSessionManager.h @@ -383,8 +383,9 @@ NS_ASSUME_NONNULL_BEGIN @param block A block object to be executed when a session task is completed. The block has no return value, and takes three arguments: the session, the task, and any metrics that were collected in the process of executing the task. */ +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 - (void)setTaskDidFinishCollectingMetricsBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, NSURLSessionTaskMetrics * _Nullable metrics))block; - +#endif ///------------------------------------------- /// @name Setting Data Task Delegate Callbacks ///------------------------------------------- diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index cd0fdf7f97..6669b3c0be 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -98,7 +98,9 @@ static dispatch_group_t url_session_manager_completion_group() { typedef NSInputStream * (^AFURLSessionTaskNeedNewBodyStreamBlock)(NSURLSession *session, NSURLSessionTask *task); typedef void (^AFURLSessionTaskDidSendBodyDataBlock)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend); typedef void (^AFURLSessionTaskDidCompleteBlock)(NSURLSession *session, NSURLSessionTask *task, NSError *error); +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 typedef void (^AFURLSessionTaskDidFinishCollectingMetricsBlock)(NSURLSession *session, NSURLSessionTask *task, NSURLSessionTaskMetrics * metrics); +#endif typedef NSURLSessionResponseDisposition (^AFURLSessionDataTaskDidReceiveResponseBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response); typedef void (^AFURLSessionDataTaskDidBecomeDownloadTaskBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask); @@ -122,7 +124,9 @@ - (instancetype)initWithTask:(NSURLSessionTask *)task; @property (nonatomic, strong) NSProgress *uploadProgress; @property (nonatomic, strong) NSProgress *downloadProgress; @property (nonatomic, copy) NSURL *downloadFileURL; +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 @property (nonatomic, strong) NSURLSessionTaskMetrics *sessionTaskMetrics; +#endif @property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading; @property (nonatomic, copy) AFURLSessionTaskProgressBlock uploadProgressBlock; @property (nonatomic, copy) AFURLSessionTaskProgressBlock downloadProgressBlock; @@ -213,7 +217,7 @@ - (void)URLSession:(__unused NSURLSession *)session self.mutableData = nil; } -#if AF_CAN_USE_AT_AVAILABLE +#if AF_CAN_USE_AT_AVAILABLE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10, macOS 10.12, watchOS 3, tvOS 10, *)) { if (self.sessionTaskMetrics) { userInfo[AFNetworkingTaskDidCompleteSessionTaskMetrics] = self.sessionTaskMetrics; @@ -269,11 +273,13 @@ - (void)URLSession:(__unused NSURLSession *)session } } +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics { self.sessionTaskMetrics = metrics; } +#endif #pragma mark - NSURLSessionDataDelegate @@ -889,9 +895,11 @@ - (void)setTaskDidCompleteBlock:(void (^)(NSURLSession *session, NSURLSessionTas self.taskDidComplete = block; } +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 - (void)setTaskDidFinishCollectingMetricsBlock:(void (^)(NSURLSession * _Nonnull, NSURLSessionTask * _Nonnull, NSURLSessionTaskMetrics * _Nullable))block { self.taskDidFinishCollectingMetrics = block; } +#endif #pragma mark - @@ -1099,6 +1107,7 @@ - (void)URLSession:(NSURLSession *)session } } +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics @@ -1113,6 +1122,7 @@ - (void)URLSession:(NSURLSession *)session self.taskDidFinishCollectingMetrics(session, task, metrics); } } +#endif #pragma mark - NSURLSessionDataDelegate diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index deb067dfe2..b34e5c0745 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -143,10 +143,12 @@ - (void)testSessionTaskDoesReportMetrics { return YES; }]; +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 __weak XCTestExpectation *metricsBlock = [self expectationWithDescription:@"Metrics completion block is called"]; [self.localManager setTaskDidFinishCollectingMetricsBlock:^(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLSessionTaskMetrics * _Nullable metrics) { [metricsBlock fulfill]; }]; +#endif NSURLSessionTask *task = [self.localManager downloadTaskWithRequest:[self bigImageURLRequest] progress:nil From 6622b05771d3c131ada1156bcdf537b6f8a9b090 Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Fri, 29 Jun 2018 13:18:36 -0700 Subject: [PATCH 6/9] Missed one use of related type --- AFNetworking/AFURLSessionManager.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 6669b3c0be..9eb14095cf 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -485,7 +485,9 @@ @interface AFURLSessionManager () @property (readwrite, nonatomic, copy) AFURLSessionTaskNeedNewBodyStreamBlock taskNeedNewBodyStream; @property (readwrite, nonatomic, copy) AFURLSessionTaskDidSendBodyDataBlock taskDidSendBodyData; @property (readwrite, nonatomic, copy) AFURLSessionTaskDidCompleteBlock taskDidComplete; +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 @property (readwrite, nonatomic, copy) AFURLSessionTaskDidFinishCollectingMetricsBlock taskDidFinishCollectingMetrics; +#endif @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidReceiveResponseBlock dataTaskDidReceiveResponse; @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidBecomeDownloadTaskBlock dataTaskDidBecomeDownloadTask; @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidReceiveDataBlock dataTaskDidReceiveData; From 54f11357d7bf925424765e0dda1d5cd242e01e25 Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Fri, 29 Jun 2018 15:25:26 -0700 Subject: [PATCH 7/9] Replaced preprocessor variable to make sure all valid targets allow NSURLSessionTaskMetrics --- AFNetworking/AFCompatibilityMacros.h | 6 ++++++ AFNetworking/AFURLSessionManager.h | 2 +- AFNetworking/AFURLSessionManager.m | 14 +++++++------- Tests/Tests/AFURLSessionManagerTests.m | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/AFNetworking/AFCompatibilityMacros.h b/AFNetworking/AFCompatibilityMacros.h index a83d134457..8adb245798 100644 --- a/AFNetworking/AFCompatibilityMacros.h +++ b/AFNetworking/AFCompatibilityMacros.h @@ -34,4 +34,10 @@ #define AF_CAN_USE_AT_AVAILABLE 0 #endif +#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || __MAC_OS_VERSION_MAX_ALLOWED >= __MAC_OS_10_12 || __WATCH_OS_MAX_VERSION_ALLOWED >= __WATCH_OS_3_0 || __TV_OS_MAX_VERSION_ALLOWED >= __TV_OS_10_0) + #define AF_CAN_INCLUDE_SESSION_TASK_METRICS 1 +#else + #define AF_CAN_INCLUDE_SESSION_TASK_METRICS 0 +#endif + #endif /* AFCompatibilityMacros_h */ diff --git a/AFNetworking/AFURLSessionManager.h b/AFNetworking/AFURLSessionManager.h index 816c02d9aa..3fa55f95db 100644 --- a/AFNetworking/AFURLSessionManager.h +++ b/AFNetworking/AFURLSessionManager.h @@ -383,7 +383,7 @@ NS_ASSUME_NONNULL_BEGIN @param block A block object to be executed when a session task is completed. The block has no return value, and takes three arguments: the session, the task, and any metrics that were collected in the process of executing the task. */ -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS - (void)setTaskDidFinishCollectingMetricsBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, NSURLSessionTaskMetrics * _Nullable metrics))block; #endif ///------------------------------------------- diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 9eb14095cf..871962f0ab 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -98,7 +98,7 @@ static dispatch_group_t url_session_manager_completion_group() { typedef NSInputStream * (^AFURLSessionTaskNeedNewBodyStreamBlock)(NSURLSession *session, NSURLSessionTask *task); typedef void (^AFURLSessionTaskDidSendBodyDataBlock)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend); typedef void (^AFURLSessionTaskDidCompleteBlock)(NSURLSession *session, NSURLSessionTask *task, NSError *error); -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS typedef void (^AFURLSessionTaskDidFinishCollectingMetricsBlock)(NSURLSession *session, NSURLSessionTask *task, NSURLSessionTaskMetrics * metrics); #endif @@ -124,7 +124,7 @@ - (instancetype)initWithTask:(NSURLSessionTask *)task; @property (nonatomic, strong) NSProgress *uploadProgress; @property (nonatomic, strong) NSProgress *downloadProgress; @property (nonatomic, copy) NSURL *downloadFileURL; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS @property (nonatomic, strong) NSURLSessionTaskMetrics *sessionTaskMetrics; #endif @property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading; @@ -217,7 +217,7 @@ - (void)URLSession:(__unused NSURLSession *)session self.mutableData = nil; } -#if AF_CAN_USE_AT_AVAILABLE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_USE_AT_AVAILABLE && AF_CAN_INCLUDE_SESSION_TASK_METRICS if (@available(iOS 10, macOS 10.12, watchOS 3, tvOS 10, *)) { if (self.sessionTaskMetrics) { userInfo[AFNetworkingTaskDidCompleteSessionTaskMetrics] = self.sessionTaskMetrics; @@ -273,7 +273,7 @@ - (void)URLSession:(__unused NSURLSession *)session } } -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics { @@ -485,7 +485,7 @@ @interface AFURLSessionManager () @property (readwrite, nonatomic, copy) AFURLSessionTaskNeedNewBodyStreamBlock taskNeedNewBodyStream; @property (readwrite, nonatomic, copy) AFURLSessionTaskDidSendBodyDataBlock taskDidSendBodyData; @property (readwrite, nonatomic, copy) AFURLSessionTaskDidCompleteBlock taskDidComplete; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS @property (readwrite, nonatomic, copy) AFURLSessionTaskDidFinishCollectingMetricsBlock taskDidFinishCollectingMetrics; #endif @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidReceiveResponseBlock dataTaskDidReceiveResponse; @@ -897,7 +897,7 @@ - (void)setTaskDidCompleteBlock:(void (^)(NSURLSession *session, NSURLSessionTas self.taskDidComplete = block; } -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS - (void)setTaskDidFinishCollectingMetricsBlock:(void (^)(NSURLSession * _Nonnull, NSURLSessionTask * _Nonnull, NSURLSessionTaskMetrics * _Nullable))block { self.taskDidFinishCollectingMetrics = block; } @@ -1109,7 +1109,7 @@ - (void)URLSession:(NSURLSession *)session } } -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index b34e5c0745..7b238194e3 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -143,7 +143,7 @@ - (void)testSessionTaskDoesReportMetrics { return YES; }]; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +#if AF_CAN_INCLUDE_SESSION_TASK_METRICS __weak XCTestExpectation *metricsBlock = [self expectationWithDescription:@"Metrics completion block is called"]; [self.localManager setTaskDidFinishCollectingMetricsBlock:^(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLSessionTaskMetrics * _Nullable metrics) { [metricsBlock fulfill]; From ca380fdcfcbb521526d216e45265a95e438708d2 Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Fri, 29 Jun 2018 16:50:35 -0700 Subject: [PATCH 8/9] Inverted preprocessor logic to check iff version exists in target --- AFNetworking/AFCompatibilityMacros.h | 6 +++--- Tests/Tests/AFURLSessionManagerTests.m | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/AFNetworking/AFCompatibilityMacros.h b/AFNetworking/AFCompatibilityMacros.h index 8adb245798..ee741c94d2 100644 --- a/AFNetworking/AFCompatibilityMacros.h +++ b/AFNetworking/AFCompatibilityMacros.h @@ -34,10 +34,10 @@ #define AF_CAN_USE_AT_AVAILABLE 0 #endif -#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || __MAC_OS_VERSION_MAX_ALLOWED >= __MAC_OS_10_12 || __WATCH_OS_MAX_VERSION_ALLOWED >= __WATCH_OS_3_0 || __TV_OS_MAX_VERSION_ALLOWED >= __TV_OS_10_0) - #define AF_CAN_INCLUDE_SESSION_TASK_METRICS 1 -#else +#if ((__IPHONE_OS_VERSION_MAX_ALLOWED && __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_10_0) || (__MAC_OS_VERSION_MAX_ALLOWED && __MAC_OS_VERSION_MAX_ALLOWED < __MAC_OS_10_12) ||(__WATCH_OS_MAX_VERSION_ALLOWED && __WATCH_OS_MAX_VERSION_ALLOWED < __WATCH_OS_3_0) ||(__TV_OS_MAX_VERSION_ALLOWED && __TV_OS_MAX_VERSION_ALLOWED < __TV_OS_10_0)) #define AF_CAN_INCLUDE_SESSION_TASK_METRICS 0 +#else + #define AF_CAN_INCLUDE_SESSION_TASK_METRICS 1 #endif #endif /* AFCompatibilityMacros_h */ diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index 7b238194e3..ebc8a296b2 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -135,7 +135,7 @@ - (void)testDownloadTaskDoesReportProgress { - (void)testSessionTaskDoesReportMetrics { [self expectationForNotification:AFNetworkingTaskDidCompleteNotification object:nil handler:^BOOL(NSNotification * _Nonnull notification) { -#if AF_CAN_USE_AT_AVAILABLE +#if AF_CAN_USE_AT_AVAILABLE && AF_CAN_INCLUDE_SESSION_TASK_METRICS if (@available(iOS 10, macOS 10.12, watchOS 3, tvOS 10, *)) { return [notification userInfo][AFNetworkingTaskDidCompleteSessionTaskMetrics] != nil; } From 1d1c0241a9c016c441e3b2e66565dcf62449cf1f Mon Sep 17 00:00:00 2001 From: Caelin Jackson-King Date: Mon, 2 Jul 2018 17:00:18 -0700 Subject: [PATCH 9/9] Replace the version numbers with raw values --- AFNetworking/AFCompatibilityMacros.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AFNetworking/AFCompatibilityMacros.h b/AFNetworking/AFCompatibilityMacros.h index ee741c94d2..cffbde2583 100644 --- a/AFNetworking/AFCompatibilityMacros.h +++ b/AFNetworking/AFCompatibilityMacros.h @@ -34,7 +34,7 @@ #define AF_CAN_USE_AT_AVAILABLE 0 #endif -#if ((__IPHONE_OS_VERSION_MAX_ALLOWED && __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_10_0) || (__MAC_OS_VERSION_MAX_ALLOWED && __MAC_OS_VERSION_MAX_ALLOWED < __MAC_OS_10_12) ||(__WATCH_OS_MAX_VERSION_ALLOWED && __WATCH_OS_MAX_VERSION_ALLOWED < __WATCH_OS_3_0) ||(__TV_OS_MAX_VERSION_ALLOWED && __TV_OS_MAX_VERSION_ALLOWED < __TV_OS_10_0)) +#if ((__IPHONE_OS_VERSION_MAX_ALLOWED && __IPHONE_OS_VERSION_MAX_ALLOWED < 100000) || (__MAC_OS_VERSION_MAX_ALLOWED && __MAC_OS_VERSION_MAX_ALLOWED < 101200) ||(__WATCH_OS_MAX_VERSION_ALLOWED && __WATCH_OS_MAX_VERSION_ALLOWED < 30000) ||(__TV_OS_MAX_VERSION_ALLOWED && __TV_OS_MAX_VERSION_ALLOWED < 100000)) #define AF_CAN_INCLUDE_SESSION_TASK_METRICS 0 #else #define AF_CAN_INCLUDE_SESSION_TASK_METRICS 1