Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 27ec2e6

Browse files
committedSep 12, 2019
Fix unbalanced background task creation in GDTCCTUploader (#3838)
1 parent f534516 commit 27ec2e6

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed
 

‎GoogleDataTransportCCTSupport/GDTCCTLibrary/GDTCCTUploader.m

+27-9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ @interface GDTCCTUploader ()
3535
@property(nullable, nonatomic, readwrite) NSURLSessionUploadTask *currentTask;
3636

3737
/** If running in the background, the current background ID. */
38-
@property(nonatomic) GDTCORBackgroundIdentifier backgroundID;
38+
@property(nonatomic) BOOL runningInBackground;
3939

4040
@end
4141

@@ -61,7 +61,6 @@ - (instancetype)init {
6161
_uploaderQueue = dispatch_queue_create("com.google.GDTCCTUploader", DISPATCH_QUEUE_SERIAL);
6262
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
6363
_uploaderSession = [NSURLSession sessionWithConfiguration:config];
64-
_backgroundID = GDTCORBackgroundIdentifierInvalid;
6564
}
6665
return self;
6766
}
@@ -86,6 +85,15 @@ - (NSURL *)defaultServerURL {
8685
}
8786

8887
- (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+
8997
dispatch_async(_uploaderQueue, ^{
9098
if (self->_currentTask || self->_currentUploadPackage) {
9199
GDTCORLogWarning(GDTCORMCWUploadFailed, @"%@",
@@ -112,9 +120,10 @@ - (void)uploadPackage:(GDTCORUploadPackage *)package {
112120
}
113121
pb_release(gdt_cct_LogResponse_fields, &logResponse);
114122
[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];
118127
}
119128
self.currentTask = nil;
120129
self.currentUploadPackage = nil;
@@ -192,12 +201,21 @@ - (void)packageExpired:(GDTCORUploadPackage *)package {
192201
#pragma mark - GDTCORLifecycleProtocol
193202

194203
- (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];
199208
}
200209
}];
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;
201219
}
202220

203221
- (void)appWillTerminate:(GDTCORApplication *)application {

0 commit comments

Comments
 (0)
Please sign in to comment.