@@ -76,11 +76,12 @@ - (instancetype)init {
7676- (void )storeEvent : (GDTEvent *)event {
7777 [self createEventDirectoryIfNotExists ];
7878
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;
8485 }
8586 }];
8687 }
@@ -111,8 +112,8 @@ - (void)storeEvent:(GDTEvent *)event {
111112 [self .uploadCoordinator forceUploadForTarget: target];
112113 }
113114
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 ) {
116117 if (@available (macOS 10.13 , iOS 11.0 , tvOS 11.0 , *)) {
117118 NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self
118119 requiringSecureCoding: YES
@@ -123,8 +124,12 @@ - (void)storeEvent:(GDTEvent *)event {
123124 [NSKeyedArchiver archiveRootObject: self toFile: [GDTStorage archivePath ]];
124125#endif
125126 }
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;
128133 }
129134 });
130135}
@@ -216,14 +221,7 @@ - (void)appWillForeground:(GDTApplication *)app {
216221}
217222
218223- (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 ;
227225 dispatch_async (_storageQueue, ^{
228226 if (@available (macOS 10.13 , iOS 11.0 , tvOS 11.0 , *)) {
229227 NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self
@@ -236,10 +234,18 @@ - (void)appWillBackground:(GDTApplication *)app {
236234#endif
237235 }
238236 });
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+ }];
239245 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;
243249 }
244250 });
245251}
0 commit comments