@@ -76,10 +76,12 @@ - (instancetype)init {
76
76
- (void )storeEvent : (GDTEvent *)event {
77
77
[self createEventDirectoryIfNotExists ];
78
78
79
- __block GDTBackgroundIdentifier bgID = GDTBackgroundIdentifierInvalid;
80
- if (_runningInBackground) {
81
- bgID = [[GDTApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
82
- [[GDTApplication sharedApplication ] endBackgroundTask: bgID];
79
+ if (_backgroundID == GDTBackgroundIdentifierInvalid) {
80
+ _backgroundID = [[GDTApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^{
81
+ if (self->_backgroundID != GDTBackgroundIdentifierInvalid) {
82
+ [[GDTApplication sharedApplication ] endBackgroundTask: self ->_backgroundID];
83
+ self->_backgroundID = GDTBackgroundIdentifierInvalid;
84
+ }
83
85
}];
84
86
}
85
87
@@ -110,7 +112,7 @@ - (void)storeEvent:(GDTEvent *)event {
110
112
}
111
113
112
114
// If running in the background, save state to disk and end the associated background task.
113
- if (bgID != GDTBackgroundIdentifierInvalid) {
115
+ if (self-> _backgroundID != GDTBackgroundIdentifierInvalid) {
114
116
if (@available (macOS 10.13 , iOS 11.0 , tvOS 11.0 , *)) {
115
117
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self
116
118
requiringSecureCoding: YES
@@ -121,7 +123,8 @@ - (void)storeEvent:(GDTEvent *)event {
121
123
[NSKeyedArchiver archiveRootObject: self toFile: [GDTStorage archivePath ]];
122
124
#endif
123
125
}
124
- [[GDTApplication sharedApplication ] endBackgroundTask: bgID];
126
+ [[GDTApplication sharedApplication ] endBackgroundTask: self ->_backgroundID];
127
+ self->_backgroundID = GDTBackgroundIdentifierInvalid;
125
128
}
126
129
});
127
130
}
@@ -210,27 +213,34 @@ - (void)appWillForeground:(GDTApplication *)app {
210
213
[NSKeyedUnarchiver unarchiveObjectWithFile: [GDTStorage archivePath ]];
211
214
#endif
212
215
}
213
- self->_runningInBackground = NO ;
214
216
}
215
217
216
218
- (void )appWillBackground : (GDTApplication *)app {
217
- self->_runningInBackground = YES ;
218
- if (@available (macOS 10.13 , iOS 11.0 , tvOS 11.0 , *)) {
219
- NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self
220
- requiringSecureCoding: YES
221
- error: nil ];
222
- [data writeToFile: [GDTStorage archivePath ] atomically: YES ];
223
- } else {
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
+ }
227
+ dispatch_async (_storageQueue, ^{
228
+ if (@available (macOS 10.13 , iOS 11.0 , tvOS 11.0 , *)) {
229
+ NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self
230
+ requiringSecureCoding: YES
231
+ error: nil ];
232
+ [data writeToFile: [GDTStorage archivePath ] atomically: YES ];
233
+ } else {
224
234
#if !defined(TARGET_OS_MACCATALYST)
225
- [NSKeyedArchiver archiveRootObject: self toFile: [GDTStorage archivePath ]];
235
+ [NSKeyedArchiver archiveRootObject: self toFile: [GDTStorage archivePath ]];
226
236
#endif
227
- }
228
- // Create an immediate background task to run until the end of the current queue of work.
229
- __block GDTBackgroundIdentifier bgID = [app beginBackgroundTaskWithExpirationHandler: ^{
230
- [app endBackgroundTask: bgID];
231
- }];
237
+ }
238
+ });
232
239
dispatch_async (_storageQueue, ^{
233
- [app endBackgroundTask: bgID];
240
+ if (self->_backgroundID != GDTBackgroundIdentifierInvalid) {
241
+ [app endBackgroundTask: self ->_backgroundID];
242
+ self->_backgroundID = GDTBackgroundIdentifierInvalid;
243
+ }
234
244
});
235
245
}
236
246
@@ -283,25 +293,19 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
283
293
284
294
- (void )encodeWithCoder : (NSCoder *)aCoder {
285
295
GDTStorage *sharedInstance = [self .class sharedInstance ];
286
- dispatch_queue_t storageQueue = sharedInstance.storageQueue ;
287
- if (!storageQueue) {
288
- return ;
296
+ NSMutableOrderedSet <GDTStoredEvent *> *storedEvents = sharedInstance->_storedEvents ;
297
+ if (storedEvents) {
298
+ [aCoder encodeObject: storedEvents forKey: kGDTStorageStoredEventsKey ];
299
+ }
300
+ NSMutableDictionary <NSNumber *, NSMutableSet <GDTStoredEvent *> *> *targetToEventSet =
301
+ sharedInstance->_targetToEventSet ;
302
+ if (targetToEventSet) {
303
+ [aCoder encodeObject: targetToEventSet forKey: kGDTStorageTargetToEventSetKey ];
304
+ }
305
+ GDTUploadCoordinator *uploadCoordinator = sharedInstance->_uploadCoordinator ;
306
+ if (uploadCoordinator) {
307
+ [aCoder encodeObject: uploadCoordinator forKey: kGDTStorageUploadCoordinatorKey ];
289
308
}
290
- dispatch_sync (storageQueue, ^{
291
- NSMutableOrderedSet <GDTStoredEvent *> *storedEvents = sharedInstance->_storedEvents ;
292
- if (storedEvents) {
293
- [aCoder encodeObject: storedEvents forKey: kGDTStorageStoredEventsKey ];
294
- }
295
- NSMutableDictionary <NSNumber *, NSMutableSet <GDTStoredEvent *> *> *targetToEventSet =
296
- sharedInstance->_targetToEventSet ;
297
- if (targetToEventSet) {
298
- [aCoder encodeObject: targetToEventSet forKey: kGDTStorageTargetToEventSetKey ];
299
- }
300
- GDTUploadCoordinator *uploadCoordinator = sharedInstance->_uploadCoordinator ;
301
- if (uploadCoordinator) {
302
- [aCoder encodeObject: uploadCoordinator forKey: kGDTStorageUploadCoordinatorKey ];
303
- }
304
- });
305
309
}
306
310
307
311
@end
0 commit comments