Skip to content

Commit

Permalink
Add background processing
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
  • Loading branch information
SystemKeeper committed Dec 9, 2023
1 parent c0b1f80 commit 46a3076
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions NextcloudTalk/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[NCRoomsManager sharedInstance];

[self registerBackgroundFetchTask];
[self registerBackgroundProcessingTask];

[NCUserInterfaceController sharedInstance].mainViewController = (NCSplitViewController *) self.window.rootViewController;
[NCUserInterfaceController sharedInstance].roomsTableViewController = [NCUserInterfaceController sharedInstance].mainViewController.viewControllers.firstObject.childViewControllers.firstObject;
Expand Down Expand Up @@ -141,6 +142,7 @@ - (void)applicationDidEnterBackground:(UIApplication *)application

[self keepExternalSignalingConnectionAliveTemporarily];
[self scheduleAppRefresh];
[self scheduleBackroundProcessing];
}


Expand Down Expand Up @@ -354,6 +356,56 @@ - (NSString *)stringWithDeviceToken:(NSData *)deviceToken
return [token copy];
}

#pragma mark - BackgroundProcessing

- (void)registerBackgroundProcessingTask {
NSString *processingTaskIdentifier = [NSString stringWithFormat:@"%@.processing", NSBundle.mainBundle.bundleIdentifier];

// see: https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler?language=objc
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:processingTaskIdentifier
usingQueue:nil
launchHandler:^(__kindof BGTask * _Nonnull task) {
[self handleBackgroundProcessing:task];
}];
}

- (void)scheduleBackroundProcessing
{
NSString *processingTaskIdentifier = [NSString stringWithFormat:@"%@.processing", NSBundle.mainBundle.bundleIdentifier];

BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:processingTaskIdentifier];
request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:UIApplicationBackgroundFetchIntervalMinimum];
request.requiresNetworkConnectivity = YES;
request.requiresExternalPower = NO;

NSError *error = nil;
[[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];

if (error) {
NSLog(@"Failed to submit background processing request: %@", error);
}
}

- (void)handleBackgroundProcessing:(BGTask *)task
{
[NCUtils log:@"Performing background processing -> handleBackgroundProcessing"];

// With BGTasks (iOS >= 13) we need to schedule another task when running in background
[self scheduleBackroundProcessing];

BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCBackgroundProcessing" expirationHandler:^(BGTaskHelper *task) {
[NCUtils log:@"ExpirationHandler NCBackgroundProcessing called"];
}];

// Check if the shown notifications are still available on the server
[[NCNotificationController sharedInstance] checkNotificationExistanceWithCompletionBlock:^(NSError *error) {
[NCUtils log:@"CompletionHandler checkNotificationExistance"];

[task setTaskCompletedWithSuccess:YES];
[bgTask stopBackgroundTask];
}];
}

#pragma mark - BackgroundFetch / AppRefresh

- (void)registerBackgroundFetchTask {
Expand Down
1 change: 1 addition & 0 deletions NextcloudTalk/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).refresh</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).processing</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
Expand Down

0 comments on commit 46a3076

Please sign in to comment.