I'm trying to get background tasks working and I have registered both a refresh and process task. I believe I have everything in place but my tasks take 10 minutes before anything happens. I thought at first it wasn't working at all until I just left it running while writing this.
I run the app from Xcode on my device and then once running I swipe up to put it in the background. I realize there is no guaranteed time for it be called. But when it allows you to pick the minimum time in seconds or even 0, what's the point? Is this because I have it running in debug mode?
Also I have my device plugged into power so it shouldn't be power savings. Is 10 minutes the minimum time before a background task is executed?
My app generates the following output after it launches and I swipe up to put it into the background...
You'll notice it takes 10 minutes + 15 seconds.
For reference, here is my code and project config below that handles all this...
I do have "Background fetch", "Remote Notifications", and "Background Processing" turned on under Background Modes in the capabilities for my project. I also have "Push Notifications" added as a capability.
For the background task identifiers in my info.plist I have the following...
In my AppDelegate.m (Using ObjC) I have the following method...
Then of course I have this in my didFinishLaunchingWithOptions...
I have this in my SceneDelegate.m to actually schedule the background tasks...
Finally, I have this these two methods which are called from above when the app goes into the background...
I run the app from Xcode on my device and then once running I swipe up to put it in the background. I realize there is no guaranteed time for it be called. But when it allows you to pick the minimum time in seconds or even 0, what's the point? Is this because I have it running in debug mode?
Also I have my device plugged into power so it shouldn't be power savings. Is 10 minutes the minimum time before a background task is executed?
My app generates the following output after it launches and I swipe up to put it into the background...
Code Block 2021-03-26 12:26:31.824989-0600 OpenURLTest[8518:4735105] Registering Background Tasks. 2021-03-26 12:26:31.825543-0600 OpenURLTest[8518:4735105] Registering Background Tasks : COMPLETE. 2021-03-26 12:26:35.834877-0600 OpenURLTest[8518:4735105] Scheduling REFRESH background task. 2021-03-26 12:26:35.837136-0600 OpenURLTest[8518:4735105] Scheduling REFRESH background task COMPLETE. 2021-03-26 12:26:35.837199-0600 OpenURLTest[8518:4735105] Scheduling PROCESSING background task. 2021-03-26 12:26:35.837597-0600 OpenURLTest[8518:4735105] Scheduling PROCESSING background task COMPLETE. 2021-03-26 12:26:35.842346-0600 OpenURLTest[8518:4735105] Background Refresh Status = 2 2021-03-26 12:36:50.341259-0600 OpenURLTest[8518:4735136] PROCESS : registerForTaskWithIdentifier Complete. BGTask = <BGProcessingTask: com.etepstudios.OpenURLTest.process> 2021-03-26 12:36:50.341391-0600 OpenURLTest[8518:4735138] REFRESH : registerForTaskWithIdentifier Complete. BGTask = <BGAppRefreshTask: com.etepstudios.OpenURLTest.refresh>
You'll notice it takes 10 minutes + 15 seconds.
For reference, here is my code and project config below that handles all this...
I do have "Background fetch", "Remote Notifications", and "Background Processing" turned on under Background Modes in the capabilities for my project. I also have "Push Notifications" added as a capability.
For the background task identifiers in my info.plist I have the following...
Code Block <key>BGTaskSchedulerPermittedIdentifiers</key> <array> <string>com.etepstudios.OpenURLTest.refresh</string> <string>com.etepstudios.OpenURLTest.process</string> </array>
In my AppDelegate.m (Using ObjC) I have the following method...
Code Block - (void) registerBackgroundTasks { NSLog(@"Registering Background Tasks."); [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.etepstudios.OpenURLTest.refresh" usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) { NSLog(@"REFRESH : registerForTaskWithIdentifier Complete. BGTask = %@", task); [task setTaskCompletedWithSuccess:YES]; }]; [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.etepstudios.OpenURLTest.process" usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) { NSLog(@"PROCESS : registerForTaskWithIdentifier Complete. BGTask = %@", task); [task setTaskCompletedWithSuccess:YES]; }]; NSLog(@"Registering Background Tasks : COMPLETE."); }
Then of course I have this in my didFinishLaunchingWithOptions...
Code Block - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self registerBackgroundTasks]; return YES; }
I have this in my SceneDelegate.m to actually schedule the background tasks...
Code Block - (void)sceneDidEnterBackground:(UIScene *)scene { [self sheduleBackgroundRefresh]; [self sheduleBackgroundProcessing]; NSLog(@"Background Refresh Status = %ld", (long)[[UIApplication sharedApplication] backgroundRefreshStatus]); }
Finally, I have this these two methods which are called from above when the app goes into the background...
Code Block - (void) sheduleBackgroundRefresh { NSLog(@"Scheduling REFRESH background task."); BGAppRefreshTaskRequest *request = [[BGAppRefreshTaskRequest alloc] initWithIdentifier:@"com.etepstudios.OpenURLTest.refresh"]; [request setEarliestBeginDate:[NSDate dateWithTimeIntervalSinceNow:3]]; NSError *error; [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error]; if (error) { NSLog(@"Error submitting REFRESH task for background : %@", error); } NSLog(@"Scheduling REFRESH background task COMPLETE."); } - (void) sheduleBackgroundProcessing { NSLog(@"Scheduling PROCESSING background task."); BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:@"com.etepstudios.OpenURLTest.process"]; [request setRequiresNetworkConnectivity:YES]; [request setRequiresExternalPower:NO]; [request setEarliestBeginDate:[NSDate dateWithTimeIntervalSinceNow:45]]; NSError *error; [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error]; if (error) { NSLog(@"Error submitting PROCESSING task for background : %@", error); } NSLog(@"Scheduling PROCESSING background task COMPLETE."); }