@@ -35,7 +35,7 @@ @interface GDTCCTUploader ()
35
35
@property (nullable , nonatomic , readwrite ) NSURLSessionUploadTask *currentTask;
36
36
37
37
/* * If running in the background, the current background ID. */
38
- @property (nonatomic ) GDTCORBackgroundIdentifier backgroundID ;
38
+ @property (nonatomic ) BOOL runningInBackground ;
39
39
40
40
@end
41
41
@@ -61,7 +61,6 @@ - (instancetype)init {
61
61
_uploaderQueue = dispatch_queue_create (" com.google.GDTCCTUploader" , DISPATCH_QUEUE_SERIAL);
62
62
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration ];
63
63
_uploaderSession = [NSURLSession sessionWithConfiguration: config];
64
- _backgroundID = GDTCORBackgroundIdentifierInvalid;
65
64
}
66
65
return self;
67
66
}
@@ -86,6 +85,15 @@ - (NSURL *)defaultServerURL {
86
85
}
87
86
88
87
- (void )uploadPackage : (GDTCORUploadPackage *)package {
88
+ GDTCORBackgroundIdentifier bgID = GDTCORBackgroundIdentifierInvalid;
89
+ if (_runningInBackground) {
90
+ bgID = [[GDTCORApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
91
+ if (bgID != GDTCORBackgroundIdentifierInvalid) {
92
+ [[GDTCORApplication sharedApplication ] endBackgroundTask: bgID];
93
+ }
94
+ }];
95
+ }
96
+
89
97
dispatch_async (_uploaderQueue, ^{
90
98
if (self->_currentTask || self->_currentUploadPackage ) {
91
99
GDTCORLogWarning (GDTCORMCWUploadFailed, @" %@ " ,
@@ -112,9 +120,10 @@ - (void)uploadPackage:(GDTCORUploadPackage *)package {
112
120
}
113
121
pb_release (gdt_cct_LogResponse_fields, &logResponse);
114
122
[package completeDelivery ];
115
- if (self->_backgroundID != GDTCORBackgroundIdentifierInvalid) {
116
- [[GDTCORApplication sharedApplication ] endBackgroundTask: self ->_backgroundID];
117
- self->_backgroundID = GDTCORBackgroundIdentifierInvalid;
123
+
124
+ // End the background task if there was one.
125
+ if (bgID != GDTCORBackgroundIdentifierInvalid) {
126
+ [[GDTCORApplication sharedApplication ] endBackgroundTask: bgID];
118
127
}
119
128
self.currentTask = nil ;
120
129
self.currentUploadPackage = nil ;
@@ -192,12 +201,21 @@ - (void)packageExpired:(GDTCORUploadPackage *)package {
192
201
#pragma mark - GDTCORLifecycleProtocol
193
202
194
203
- (void )appWillBackground : (GDTCORApplication *)app {
195
- _backgroundID = [app beginBackgroundTaskWithExpirationHandler: ^{
196
- if (self-> _backgroundID != GDTCORBackgroundIdentifierInvalid) {
197
- [app endBackgroundTask: self ->_backgroundID];
198
- self-> _backgroundID = GDTCORBackgroundIdentifierInvalid ;
204
+ _runningInBackground = YES ;
205
+ __block GDTCORBackgroundIdentifier bgID = [app beginBackgroundTaskWithExpirationHandler: ^ {
206
+ if (bgID != GDTCORBackgroundIdentifierInvalid) {
207
+ [app endBackgroundTask: bgID] ;
199
208
}
200
209
}];
210
+ if (bgID != GDTCORBackgroundIdentifierInvalid) {
211
+ dispatch_async (_uploaderQueue, ^{
212
+ [[GDTCORApplication sharedApplication ] endBackgroundTask: bgID];
213
+ });
214
+ }
215
+ }
216
+
217
+ - (void )appWillForeground : (GDTCORApplication *)app {
218
+ _runningInBackground = NO ;
201
219
}
202
220
203
221
- (void )appWillTerminate : (GDTCORApplication *)application {
0 commit comments