Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background processing #1449

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading