Backgroud task never execute on watch

Hi everyone! I'm writing a watch app using backgroud refresh. But the backround task was not triggered either on simulator or real watch device.

Answered by DTS Engineer in 894693022

Hi everyone! I'm writing a watch app using background refresh. But the background task was not triggered either on the simulator or real watch device.

As written, I would not expect your code to work, at least not consistently. The key issue is here:

func applicationDidFinishLaunching() {
...
	BackgroundRefreshManager.shared.scheduleBackgroundRefresh(BackgroundRefreshManager.backgroundRefreshDelay)
}

The problem is that by scheduling at launch like this, you're unintentionally preventing your task from running any time it's launched into the background. See this forum post for a more complete description of how this plays out. Note that BGAppRefreshTask is what actually implements scheduleBackgroundRefresh, so the behavior will be exactly the same.

A few other notes:

  1. Refresh tasks will NEVER fire while you're debugging your app. Xcode is keeping your app awake, which means there's no reason for a refresh task to fire. This is the same reason a task won't occur while your app is in the foreground.

  2. For development/testing purposes, you can use the process described in "Starting and Terminating Tasks During Development" to force tasks to fire while your app is running in Xcode, allowing you to debug and test your app’s logic flow.

  3. For real-world testing, make sure you don't force-quit your app, as this will prevent tasks from firing. My recommendation is that you run/install the app with Xcode, stop the app in Xcode, then manually launch the app on the watch, as this is the simplest flow that ensures your app is running "normally".

  4. I'd also suggest reviewing this post which describes some of the scheduling oddities which can happen with this API during development.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Thanks for the post. I’m not the best person to answer watchOS questions, but this one looking in your code should be straightforward.

The watchOS simulator does not run the background task scheduler the same way a real device does. If your app does not have a complication on the user's active watch face, your background execution budget is extremely limited. I would recommend to add a complication for your app to your active watch face and try again. Make sure in your project that in Signing & Capabilities tab. If you don't see "Background Modes", click the + Capability button and add it. Check the box for background fetch

I don’t see in your code the import nor the WKApplicationDelegate nor you scheduling a task with using WKExtension.shared().scheduleBackgroundRefresh(…)?

On a real Apple Watch, the system will suppress background tasks if the watch battery is low, low Power Mode is turned on. Try the Debug > Simulate Background Fetch in Xcode first to ensure your handle(_ backgroundTasks:) code is wired up correctly. If that works, add your complication to the watch face, schedule a task for 15 minutes in the future, put the app in the background, and wait.

Hope this helps

Albert  WWDR

Hi everyone! I'm writing a watch app using background refresh. But the background task was not triggered either on the simulator or real watch device.

As written, I would not expect your code to work, at least not consistently. The key issue is here:

func applicationDidFinishLaunching() {
...
	BackgroundRefreshManager.shared.scheduleBackgroundRefresh(BackgroundRefreshManager.backgroundRefreshDelay)
}

The problem is that by scheduling at launch like this, you're unintentionally preventing your task from running any time it's launched into the background. See this forum post for a more complete description of how this plays out. Note that BGAppRefreshTask is what actually implements scheduleBackgroundRefresh, so the behavior will be exactly the same.

A few other notes:

  1. Refresh tasks will NEVER fire while you're debugging your app. Xcode is keeping your app awake, which means there's no reason for a refresh task to fire. This is the same reason a task won't occur while your app is in the foreground.

  2. For development/testing purposes, you can use the process described in "Starting and Terminating Tasks During Development" to force tasks to fire while your app is running in Xcode, allowing you to debug and test your app’s logic flow.

  3. For real-world testing, make sure you don't force-quit your app, as this will prevent tasks from firing. My recommendation is that you run/install the app with Xcode, stop the app in Xcode, then manually launch the app on the watch, as this is the simplest flow that ensures your app is running "normally".

  4. I'd also suggest reviewing this post which describes some of the scheduling oddities which can happen with this API during development.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Backgroud task never execute on watch
 
 
Q