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 ]];
@@ -157,6 +163,8 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
157
163
return nil ;
158
164
}
159
165
166
+ _URLSession = session;
167
+
160
168
// Save the session into memory.
161
169
[[self class ] setSessionInFetcherMap: self forSessionID: _sessionID];
162
170
@@ -199,6 +207,8 @@ - (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
199
207
return nil ;
200
208
}
201
209
210
+ _URLSession = session;
211
+
202
212
// Save the session into memory.
203
213
[[self class ] setSessionInFetcherMap: self forSessionID: _sessionID];
204
214
@@ -283,16 +293,16 @@ - (void)URLSession:(NSURLSession *)session
283
293
[self maybeRemoveTempFilesAtURL: _networkDirectoryURL
284
294
expiringTime: kGULNetworkTempFolderExpireTime ];
285
295
286
- // Invalidate the session only if it's owned by this class.
287
296
NSString *sessionID = session.configuration .identifier ;
288
- if ([sessionID hasPrefix: kGULNetworkBackgroundSessionConfigIDPrefix ]) {
289
- [session finishTasksAndInvalidate ];
290
297
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
- }
298
+ // This is called without checking the sessionID here since non-background sessions
299
+ // won't have an ID.
300
+ [session finishTasksAndInvalidate ];
301
+
302
+ // Explicitly remove the session so it won't be reused. The weak map table should
303
+ // remove the session on deallocation, but dealloc may not happen immediately after
304
+ // calling `finishTasksAndInvalidate`.
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 != nil ) {
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