@@ -76,11 +76,12 @@ - (instancetype)init {
76
76
- (void )storeEvent : (GDTEvent *)event {
77
77
[self createEventDirectoryIfNotExists ];
78
78
79
- if (_backgroundID == GDTBackgroundIdentifierInvalid) {
80
- _backgroundID = [[GDTApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
81
- if (self->_backgroundID != GDTBackgroundIdentifierInvalid) {
82
- [[GDTApplication sharedApplication ] endBackgroundTask: self ->_backgroundID];
83
- self->_backgroundID = GDTBackgroundIdentifierInvalid;
79
+ __block GDTBackgroundIdentifier bgID = GDTBackgroundIdentifierInvalid;
80
+ if (_runningInBackground) {
81
+ bgID = [[GDTApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
82
+ if (bgID != GDTBackgroundIdentifierInvalid) {
83
+ [[GDTApplication sharedApplication ] endBackgroundTask: bgID];
84
+ bgID = GDTBackgroundIdentifierInvalid;
84
85
}
85
86
}];
86
87
}
@@ -111,8 +112,8 @@ - (void)storeEvent:(GDTEvent *)event {
111
112
[self .uploadCoordinator forceUploadForTarget: target];
112
113
}
113
114
114
- // If running in the background, save state to disk and end the associated background task .
115
- if (self->_backgroundID != GDTBackgroundIdentifierInvalid ) {
115
+ // Write state to disk.
116
+ if (self->_runningInBackground ) {
116
117
if (@available (macOS 10.13 , iOS 11.0 , tvOS 11.0 , *)) {
117
118
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self
118
119
requiringSecureCoding: YES
@@ -123,8 +124,12 @@ - (void)storeEvent:(GDTEvent *)event {
123
124
[NSKeyedArchiver archiveRootObject: self toFile: [GDTStorage archivePath ]];
124
125
#endif
125
126
}
126
- [[GDTApplication sharedApplication ] endBackgroundTask: self ->_backgroundID];
127
- self->_backgroundID = GDTBackgroundIdentifierInvalid;
127
+ }
128
+
129
+ // If running in the background, save state to disk and end the associated background task.
130
+ if (bgID != GDTBackgroundIdentifierInvalid) {
131
+ [[GDTApplication sharedApplication ] endBackgroundTask: bgID];
132
+ bgID = GDTBackgroundIdentifierInvalid;
128
133
}
129
134
});
130
135
}
@@ -216,14 +221,7 @@ - (void)appWillForeground:(GDTApplication *)app {
216
221
}
217
222
218
223
- (void )appWillBackground : (GDTApplication *)app {
219
- if (_backgroundID == GDTBackgroundIdentifierInvalid) {
220
- _backgroundID = [[GDTApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
221
- if (self->_backgroundID != GDTBackgroundIdentifierInvalid) {
222
- [[GDTApplication sharedApplication ] endBackgroundTask: self ->_backgroundID];
223
- self->_backgroundID = GDTBackgroundIdentifierInvalid;
224
- }
225
- }];
226
- }
224
+ self->_runningInBackground = YES ;
227
225
dispatch_async (_storageQueue, ^{
228
226
if (@available (macOS 10.13 , iOS 11.0 , tvOS 11.0 , *)) {
229
227
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self
@@ -236,10 +234,18 @@ - (void)appWillBackground:(GDTApplication *)app {
236
234
#endif
237
235
}
238
236
});
237
+
238
+ // Create an immediate background task to run until the end of the current queue of work.
239
+ __block GDTBackgroundIdentifier bgID = [app beginBackgroundTaskWithExpirationHandler: ^{
240
+ if (bgID != GDTBackgroundIdentifierInvalid) {
241
+ [app endBackgroundTask: bgID];
242
+ bgID = GDTBackgroundIdentifierInvalid;
243
+ }
244
+ }];
239
245
dispatch_async (_storageQueue, ^{
240
- if (self-> _backgroundID != GDTBackgroundIdentifierInvalid) {
241
- [app endBackgroundTask: self ->_backgroundID ];
242
- self-> _backgroundID = GDTBackgroundIdentifierInvalid;
246
+ if (bgID != GDTBackgroundIdentifierInvalid) {
247
+ [app endBackgroundTask: bgID ];
248
+ bgID = GDTBackgroundIdentifierInvalid;
243
249
}
244
250
});
245
251
}
0 commit comments