21
21
#import " Private/GULNetworkConstants.h"
22
22
#import " Private/GULNetworkMessageCode.h"
23
23
24
+ @interface GULNetworkURLSession () <NSURLSessionDelegate , NSURLSessionTaskDelegate ,
25
+ NSURLSessionDownloadDelegate >
26
+ @end
27
+
24
28
@implementation GULNetworkURLSession {
25
29
// / The handler to be called when the request completes or error has occurs.
26
30
GULNetworkURLSessionCompletionHandler _completionHandler;
@@ -45,6 +49,9 @@ @implementation GULNetworkURLSession {
45
49
46
50
// / The current request.
47
51
NSURLRequest *_request;
52
+
53
+ // / The current NSURLSession.
54
+ NSURLSession *__weak _Nullable _URLSession;
48
55
}
49
56
50
57
#pragma mark - Init
@@ -128,7 +135,7 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
128
135
129
136
if (didWriteFile) {
130
137
// Exclude this file from backing up to iTunes. There are conflicting reports that excluding
131
- // directory from backing up does not excluding files of that directory from backing up.
138
+ // directory from backing up does not exclude files of that directory from backing up.
132
139
[self excludeFromBackupForURL: _uploadingFileURL];
133
140
134
141
_sessionConfig = [self backgroundSessionConfigWithSessionID: _sessionID];
@@ -141,7 +148,6 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
141
148
// If we cannot write to file, just send it in the foreground.
142
149
_sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration ];
143
150
[self populateSessionConfig: _sessionConfig withRequest: request];
144
- _sessionConfig.URLCache = nil ;
145
151
session = [NSURLSession sessionWithConfiguration: _sessionConfig
146
152
delegate: self
147
153
delegateQueue: [NSOperationQueue mainQueue ]];
@@ -199,6 +205,8 @@ - (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
199
205
return nil ;
200
206
}
201
207
208
+ _URLSession = session;
209
+
202
210
// Save the session into memory.
203
211
[[self class ] setSessionInFetcherMap: self forSessionID: _sessionID];
204
212
@@ -283,16 +291,16 @@ - (void)URLSession:(NSURLSession *)session
283
291
[self maybeRemoveTempFilesAtURL: _networkDirectoryURL
284
292
expiringTime: kGULNetworkTempFolderExpireTime ];
285
293
286
- // Invalidate the session only if it's owned by this class.
287
294
NSString *sessionID = session.configuration .identifier ;
288
- if ([sessionID hasPrefix: kGULNetworkBackgroundSessionConfigIDPrefix ]) {
289
- [session finishTasksAndInvalidate ];
290
295
291
- // Explicitly remove the session so it won't be reused. The weak map table should
292
- // remove the session on deallocation, but dealloc may not happen immediately after
293
- // calling `finishTasksAndInvalidate`.
294
- [[self class ] setSessionInFetcherMap: nil forSessionID: sessionID];
295
- }
296
+ // This is called without checking the sessionID here since non-background sessions
297
+ // won't have an ID.
298
+ [session finishTasksAndInvalidate ];
299
+
300
+ // Explicitly remove the session so it won't be reused. The weak map table should
301
+ // remove the session on deallocation, but dealloc may not happen immediately after
302
+ // calling `finishTasksAndInvalidate`.
303
+ [[self class ] setSessionInFetcherMap: nil forSessionID: sessionID];
296
304
}
297
305
298
306
- (void )URLSession : (NSURLSession *)session
@@ -677,13 +685,13 @@ + (void)setSessionInFetcherMap:(GULNetworkURLSession *)session forSessionID:(NSS
677
685
GULNetworkURLSession *existingSession =
678
686
[[[self class ] sessionIDToFetcherMap ] objectForKey: sessionID];
679
687
if (existingSession) {
680
- // Invalidating doesn't seem like the right thing to do here since it may cancel an active
681
- // background transfer if the background session is handling multiple requests. The old
682
- // session will be dropped from the map table, but still complete its request.
683
- NSString *message = [ NSString stringWithFormat: @" Discarding session: %@ " , existingSession];
684
- [existingSession->_loggerDelegate GULNetwork_logWithLevel: kGULNetworkLogLevelInfo
685
- messageCode: kGULNetworkMessageCodeURLSession019
686
- message: message ];
688
+ if (session != nil ) {
689
+ NSString *message = [ NSString stringWithFormat: @" Discarding session: %@ " , existingSession];
690
+ [existingSession->_loggerDelegate GULNetwork_logWithLevel: kGULNetworkLogLevelInfo
691
+ messageCode: kGULNetworkMessageCodeURLSession019
692
+ message: message];
693
+ }
694
+ [existingSession->_URLSession finishTasksAndInvalidate ];
687
695
}
688
696
if (session) {
689
697
[[[self class ] sessionIDToFetcherMap ] setObject: session forKey: sessionID];
0 commit comments