21
21
#import " Private/GULNetworkConstants.h"
22
22
#import " Private/GULNetworkMessageCode.h"
23
23
24
+ @interface GULNetworkURLSession () <NSURLSessionDelegate ,
25
+ NSURLSessionTaskDelegate ,
26
+ NSURLSessionDownloadDelegate >
27
+ @end
28
+
24
29
@implementation GULNetworkURLSession {
25
30
// / The handler to be called when the request completes or error has occurs.
26
31
GULNetworkURLSessionCompletionHandler _completionHandler;
@@ -45,6 +50,9 @@ @implementation GULNetworkURLSession {
45
50
46
51
// / The current request.
47
52
NSURLRequest *_request;
53
+
54
+ // / The current NSURLSession.
55
+ NSURLSession *__weak _Nullable _URLSession;
48
56
}
49
57
50
58
#pragma mark - Init
@@ -128,7 +136,7 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
128
136
129
137
if (didWriteFile) {
130
138
// 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.
139
+ // directory from backing up does not exclude files of that directory from backing up.
132
140
[self excludeFromBackupForURL: _uploadingFileURL];
133
141
134
142
_sessionConfig = [self backgroundSessionConfigWithSessionID: _sessionID];
@@ -141,7 +149,6 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
141
149
// If we cannot write to file, just send it in the foreground.
142
150
_sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration ];
143
151
[self populateSessionConfig: _sessionConfig withRequest: request];
144
- _sessionConfig.URLCache = nil ;
145
152
session = [NSURLSession sessionWithConfiguration: _sessionConfig
146
153
delegate: self
147
154
delegateQueue: [NSOperationQueue mainQueue ]];
@@ -157,6 +164,8 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
157
164
return nil ;
158
165
}
159
166
167
+ _URLSession = session;
168
+
160
169
// Save the session into memory.
161
170
[[self class ] setSessionInFetcherMap: self forSessionID: _sessionID];
162
171
@@ -199,6 +208,8 @@ - (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
199
208
return nil ;
200
209
}
201
210
211
+ _URLSession = session;
212
+
202
213
// Save the session into memory.
203
214
[[self class ] setSessionInFetcherMap: self forSessionID: _sessionID];
204
215
@@ -283,16 +294,15 @@ - (void)URLSession:(NSURLSession *)session
283
294
[self maybeRemoveTempFilesAtURL: _networkDirectoryURL
284
295
expiringTime: kGULNetworkTempFolderExpireTime ];
285
296
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 ];
297
+ // This is called without checking the sessionID here since non-background sessions
298
+ // won't have an ID.
299
+ [session finishTasksAndInvalidate ];
290
300
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
- }
301
+ // Explicitly remove the session so it won't be reused. The weak map table should
302
+ // remove the session on deallocation, but dealloc may not happen immediately after
303
+ // calling `finishTasksAndInvalidate`.
304
+ NSString *sessionID = session. configuration . identifier ;
305
+ [[ self class ] setSessionInFetcherMap: nil forSessionID: sessionID];
296
306
}
297
307
298
308
- (void )URLSession : (NSURLSession *)session
@@ -677,13 +687,13 @@ + (void)setSessionInFetcherMap:(GULNetworkURLSession *)session forSessionID:(NSS
677
687
GULNetworkURLSession *existingSession =
678
688
[[[self class ] sessionIDToFetcherMap ] objectForKey: sessionID];
679
689
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 ];
690
+ if (session) {
691
+ NSString *message = [ NSString stringWithFormat: @" Discarding session: %@ " , existingSession];
692
+ [existingSession->_loggerDelegate GULNetwork_logWithLevel: kGULNetworkLogLevelInfo
693
+ messageCode: kGULNetworkMessageCodeURLSession019
694
+ message: message];
695
+ }
696
+ [existingSession->_URLSession finishTasksAndInvalidate ];
687
697
}
688
698
if (session) {
689
699
[[[self class ] sessionIDToFetcherMap ] setObject: session forKey: sessionID];
0 commit comments