@@ -32,7 +32,7 @@ @implementation GULNetworkURLSession {
32
32
#pragma clang diagnostic ignored "-Wunguarded-availability"
33
33
// / The session configuration. NSURLSessionConfiguration' is only available on iOS 7.0 or newer.
34
34
NSURLSessionConfiguration *_sessionConfig;
35
- #pragma pop
35
+ #pragma clang diagnostic pop
36
36
37
37
// / The path to the directory where all temporary files are stored before uploading.
38
38
NSURL *_networkDirectoryURL;
@@ -158,8 +158,7 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
158
158
}
159
159
160
160
// Save the session into memory.
161
- NSMapTable *sessionIdentifierToFetcherMap = [[self class ] sessionIDToFetcherMap ];
162
- [sessionIdentifierToFetcherMap setObject: self forKey: _sessionID];
161
+ [self setSessionInFetcherMap: session forSessionID: _sessionID];
163
162
164
163
_request = [request copy ];
165
164
@@ -201,8 +200,7 @@ - (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
201
200
}
202
201
203
202
// Save the session into memory.
204
- NSMapTable *sessionIdentifierToFetcherMap = [[self class ] sessionIDToFetcherMap ];
205
- [sessionIdentifierToFetcherMap setObject: self forKey: _sessionID];
203
+ [self setSessionInFetcherMap: session forSessionID: _sessionID];
206
204
207
205
_request = [request copy ];
208
206
@@ -284,6 +282,12 @@ - (void)URLSession:(NSURLSession *)session
284
282
// Try to clean up stale files again.
285
283
[self maybeRemoveTempFilesAtURL: _networkDirectoryURL
286
284
expiringTime: kGULNetworkTempFolderExpireTime ];
285
+
286
+ // Invalidate the session only if it's owned by this class.
287
+ NSString *sessionID = session.configuration .identifier ;
288
+ if ([sessionID hasPrefix: kGULNetworkBackgroundSessionConfigIDPrefix ]) {
289
+ [session finishTasksAndInvalidate ];
290
+ }
287
291
}
288
292
289
293
- (void )URLSession : (NSURLSession *)session
@@ -651,6 +655,28 @@ - (void)URLSession:(NSURLSession *)session
651
655
652
656
#pragma mark - Helper Methods
653
657
658
+ - (void )setSessionInFetcherMap : (NSURLSession *)session forSessionID : (NSString *)sessionID {
659
+ NSURLSession *existingSession = [self sessionFromFetcherMapForSessionID: sessionID];
660
+ if (existingSession) {
661
+ // Invalidating doesn't seem like the right thing to do here since it may cancel an active
662
+ // background transfer if the background session is handling multiple requests. The old
663
+ // session will be dropped from the map table, but still complete its request.
664
+ NSString *message = [NSString stringWithFormat: @" Discarding URL session: %@ " , existingSession];
665
+ [_loggerDelegate GULNetwork_logWithLevel: kGULNetworkLogLevelInfo
666
+ messageCode: kGULNetworkMessageCodeURLSession019
667
+ message: message];
668
+ }
669
+ if (session) {
670
+ [[[self class ] sessionIDToFetcherMap ] setObject: session forKey: sessionID];
671
+ } else {
672
+ [[[self class ] sessionIDToFetcherMap ] removeObjectForKey: sessionID];
673
+ }
674
+ }
675
+
676
+ - (nullable NSURLSession *)sessionFromFetcherMapForSessionID : (NSString *)sessionID {
677
+ return [[[self class ] sessionIDToFetcherMap ] objectForKey: sessionID];
678
+ }
679
+
654
680
- (void )callCompletionHandler : (GULNetworkURLSessionCompletionHandler)handler
655
681
withResponse : (NSHTTPURLResponse *)response
656
682
data : (NSData *)data
@@ -669,6 +695,7 @@ - (void)callCompletionHandler:(GULNetworkURLSessionCompletionHandler)handler
669
695
}
670
696
}
671
697
698
+ // Always use the request parameters even if the default session configuration is more restrictive.
672
699
- (void )populateSessionConfig : (NSURLSessionConfiguration *)sessionConfig
673
700
withRequest : (NSURLRequest *)request API_AVAILABLE(ios(7.0 )) {
674
701
sessionConfig.HTTPAdditionalHeaders = request.allHTTPHeaderFields ;
0 commit comments