From 864c8d6aedf6c5fbcf60f6f480ca886f74c61ff1 Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Tue, 11 Sep 2018 12:15:01 -0700 Subject: [PATCH] Update PR feedback, add more tests --- AFNetworking/AFURLSessionManager.h | 10 +++++++++- AFNetworking/AFURLSessionManager.m | 12 ++++++++++-- Tests/Tests/AFURLSessionManagerTests.m | 16 ++++++++++++---- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/AFNetworking/AFURLSessionManager.h b/AFNetworking/AFURLSessionManager.h index 3fa55f95db..4b54aad123 100644 --- a/AFNetworking/AFURLSessionManager.h +++ b/AFNetworking/AFURLSessionManager.h @@ -196,7 +196,15 @@ NS_ASSUME_NONNULL_BEGIN @param cancelPendingTasks Whether or not to cancel pending tasks. */ -- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks; +- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks DEPRECATED_ATTRIBUTE; + +/** + Invalidates the managed session, optionally canceling pending tasks and optionally resets given session. + + @param cancelPendingTasks Whether or not to cancel pending tasks. + @param resetSession Whether or not to reset the session of the manager. + */ +- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession; ///------------------------- /// @name Running Data Tasks diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 1f3ad16db6..744d20ebba 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -404,7 +404,8 @@ + (void)load { 7) If the current class implementation of `resume` is not equal to the super class implementation of `resume` AND the current implementation of `resume` is not equal to the original implementation of `af_resume`, THEN swizzle the methods 8) Set the current class to the super class, and repeat steps 3-8 */ - NSURLSession * session = [NSURLSession sharedSession]; + NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; + NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnonnull" NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil]; @@ -424,6 +425,7 @@ + (void)load { } [localDataTask cancel]; + [session finishTasksAndInvalidate]; } } @@ -724,12 +726,18 @@ - (NSArray *)downloadTasks { #pragma mark - - (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks { + [self invalidateSessionCancelingTasks:cancelPendingTasks resetSession:NO]; +} + +- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession { if (cancelPendingTasks) { [self.session invalidateAndCancel]; } else { [self.session finishTasksAndInvalidate]; } - self.session = nil; + if (resetSession) { + self.session = nil; + } } #pragma mark - diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index 7207622cd1..b779347eba 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -70,10 +70,10 @@ - (void)setUp { - (void)tearDown { [super tearDown]; [self.localManager.session.configuration.URLCache removeAllCachedResponses]; - [self.localManager invalidateSessionCancelingTasks:YES]; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:YES]; self.localManager = nil; - [self.backgroundManager invalidateSessionCancelingTasks:YES]; + [self.backgroundManager invalidateSessionCancelingTasks:YES resetSession:YES]; self.backgroundManager = nil; } @@ -158,11 +158,19 @@ - (void)testSessionTaskDoesReportMetrics { [self waitForExpectationsWithCommonTimeout]; } +- (void)testSessionIsStillValid { + + NSURLSession *session = self.localManager.session; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; + + XCTAssertEqual(session, self.localManager.session); +} + - (void)testSessionRecreatesAgain { - [self.localManager setValue:nil forKey:@"session"]; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; - XCTAssertNotNil([self.localManager valueForKey:@"session"]); + XCTAssertNotNil(self.localManager.session); } - (void)testUploadTaskDoesReportProgress {