@@ -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 ) GDTBackgroundIdentifier 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 = GDTBackgroundIdentifierInvalid;
65
64
}
66
65
return self;
67
66
}
@@ -86,6 +85,15 @@ - (NSURL *)defaultServerURL {
86
85
}
87
86
88
87
- (void )uploadPackage : (GDTUploadPackage *)package {
88
+ GDTBackgroundIdentifier bgID = GDTBackgroundIdentifierInvalid;
89
+ if (_runningInBackground) {
90
+ bgID = [[GDTApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
91
+ if (bgID != GDTBackgroundIdentifierInvalid) {
92
+ [[GDTApplication sharedApplication ] endBackgroundTask: bgID];
93
+ }
94
+ }];
95
+ }
96
+
89
97
dispatch_async (_uploaderQueue, ^{
90
98
if (self->_currentTask || self->_currentUploadPackage ) {
91
99
GDTLogWarning (GDTMCWUploadFailed, @" %@ " ,
@@ -112,9 +120,10 @@ - (void)uploadPackage:(GDTUploadPackage *)package {
112
120
}
113
121
pb_release (gdt_cct_LogResponse_fields, &logResponse);
114
122
[package completeDelivery ];
115
- if (self->_backgroundID != GDTBackgroundIdentifierInvalid) {
116
- [[GDTApplication sharedApplication ] endBackgroundTask: self ->_backgroundID];
117
- self->_backgroundID = GDTBackgroundIdentifierInvalid;
123
+
124
+ // End the background task if there was one.
125
+ if (bgID != GDTBackgroundIdentifierInvalid) {
126
+ [[GDTApplication sharedApplication ] endBackgroundTask: bgID];
118
127
}
119
128
self.currentTask = nil ;
120
129
self.currentUploadPackage = nil ;
@@ -191,12 +200,21 @@ - (void)packageExpired:(GDTUploadPackage *)package {
191
200
#pragma mark - GDTLifecycleProtocol
192
201
193
202
- (void )appWillBackground : (GDTApplication *)app {
194
- _backgroundID = [app beginBackgroundTaskWithExpirationHandler: ^{
195
- if (self-> _backgroundID != GDTBackgroundIdentifierInvalid) {
196
- [app endBackgroundTask: self ->_backgroundID];
197
- self-> _backgroundID = GDTBackgroundIdentifierInvalid ;
203
+ _runningInBackground = YES ;
204
+ __block GDTBackgroundIdentifier bgID = [app beginBackgroundTaskWithExpirationHandler: ^ {
205
+ if (bgID != GDTBackgroundIdentifierInvalid) {
206
+ [app endBackgroundTask: bgID] ;
198
207
}
199
208
}];
209
+ if (bgID != GDTBackgroundIdentifierInvalid) {
210
+ dispatch_async (_uploaderQueue, ^{
211
+ [[GDTApplication sharedApplication ] endBackgroundTask: bgID];
212
+ });
213
+ }
214
+ }
215
+
216
+ - (void )appWillForeground : (GDTApplication *)app {
217
+ _runningInBackground = NO ;
200
218
}
201
219
202
220
- (void )appWillTerminate : (GDTApplication *)application {
0 commit comments