Skip to content

Commit 0fb5681

Browse files
Update app background state when starting/ending bg tasks
All tasks starting and ending should check the current background state to make sure we are always in the correct state when deciding what to do next. We want to update the state on bg task expiry, too, to make sure the expiry wasn't caused by a wrong state.
1 parent 583c4be commit 0fb5681

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Monal/Classes/MonalAppDelegate.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,8 @@ -(void) checkIfBackgroundTaskIsStillNeeded
13341334
-(void) addBackgroundTask
13351335
{
13361336
[HelperTools dispatchAsync:NO reentrantOnQueue:dispatch_get_main_queue() withBlock:^{
1337+
[self updateBackgroundState]; //make sure we are in the correct state
1338+
13371339
//don't start uikit bg task if it's already running
13381340
if(self->_bgTask != UIBackgroundTaskInvalid)
13391341
DDLogVerbose(@"Not starting UIKit background task, already running: %d", (int)self->_bgTask);
@@ -1344,6 +1346,7 @@ -(void) addBackgroundTask
13441346
self->_bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
13451347
DDLogWarn(@"BG WAKE EXPIRING");
13461348
[DDLog flushLog];
1349+
[self updateBackgroundState]; //make sure we are in the correct state
13471350

13481351
@synchronized(self) {
13491352
//ui background tasks expire at the same time as background processing/refreshing tasks
@@ -1398,6 +1401,7 @@ -(void) handleBackgroundProcessingTask:(BGTask*) task
13981401
strongify(task);
13991402
DDLogWarn(@"*** BGPROCESSING EXPIRED ***");
14001403
[DDLog flushLog];
1404+
[self updateBackgroundState]; //make sure we are in the correct state
14011405

14021406
DDLogVerbose(@"Dispatching to main queue...");
14031407
[HelperTools dispatchAsync:NO reentrantOnQueue:dispatch_get_main_queue() withBlock:^{
@@ -1502,6 +1506,7 @@ -(void) handleBackgroundRefreshingTask:(BGTask*) task
15021506
strongify(task);
15031507
DDLogWarn(@"*** BGREFRESHING EXPIRED ***");
15041508
[DDLog flushLog];
1509+
[self updateBackgroundState]; //make sure we are in the correct state
15051510

15061511
DDLogVerbose(@"Dispatching to main queue...");
15071512
[HelperTools dispatchAsync:NO reentrantOnQueue:dispatch_get_main_queue() withBlock:^{
@@ -1594,6 +1599,7 @@ -(void) configureBackgroundTasks
15941599
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kBackgroundProcessingTask usingQueue:dispatch_get_main_queue() launchHandler:^(BGTask *task) {
15951600
//resume logging and other core tasks
15961601
[HelperTools signalResumption];
1602+
[self updateBackgroundState]; //make sure we are in the correct state
15971603

15981604
DDLogDebug(@"RUNNING BGPROCESSING LAUNCH HANDLER");
15991605
DDLogInfo(@"BG time available: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
@@ -1617,6 +1623,7 @@ -(void) configureBackgroundTasks
16171623
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kBackgroundRefreshingTask usingQueue:dispatch_get_main_queue() launchHandler:^(BGTask *task) {
16181624
//resume logging and other core tasks
16191625
[HelperTools signalResumption];
1626+
[self updateBackgroundState]; //make sure we are in the correct state
16201627

16211628
DDLogDebug(@"RUNNING BGREFRESHING LAUNCH HANDLER");
16221629
DDLogInfo(@"BG time available: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
@@ -1697,6 +1704,7 @@ -(void) incomingWakeupWithCompletionHandler:(void (^)(UIBackgroundFetchResult re
16971704
{
16981705
//resume logging and other core tasks
16991706
[HelperTools signalResumption];
1707+
[self updateBackgroundState]; //make sure we are in the correct state
17001708

17011709
if(![HelperTools isInBackground])
17021710
{
@@ -1732,6 +1740,7 @@ -(void) incomingWakeupWithCompletionHandler:(void (^)(UIBackgroundFetchResult re
17321740
@"timer": createTimer(GRACEFUL_TIMEOUT, (^{
17331741
DDLogWarn(@"### Wakeup timer triggered for ID %@ ###", completionId);
17341742
dispatch_async(dispatch_get_main_queue(), ^{
1743+
[self updateBackgroundState]; //make sure we are in the correct state
17351744
@synchronized(self) {
17361745
DDLogInfo(@"Handling wakeup completion %@", completionId);
17371746
BOOL background = [HelperTools isInBackground];
@@ -1786,6 +1795,16 @@ -(void) incomingWakeupWithCompletionHandler:(void (^)(UIBackgroundFetchResult re
17861795
}
17871796
}
17881797

1798+
-(void) updateBackgroundState
1799+
{
1800+
[HelperTools dispatchAsync:NO reentrantOnQueue:dispatch_get_main_queue() withBlock:^{
1801+
if([UIApplication sharedApplication].applicationState==UIApplicationStateBackground)
1802+
[[MLXMPPManager sharedInstance] nowBackgrounded];
1803+
else
1804+
[[MLXMPPManager sharedInstance] nowForegrounded];
1805+
}];
1806+
}
1807+
17891808

17901809
#pragma mark - share sheet added
17911810

0 commit comments

Comments
 (0)