Background tasks no longer working since update to watchOS7

I have a background task from the Apple Watch that saves some data to a server every 30 - 60 minutes. It was working flawlessly on watchOS6 but stopped working on watchOS7. Strangely if I launch from Xcode on my device it works flawlessly again until it loses connection with the debugger. No background activities work when launched via TestFlight. I am not using the WKURLSessionRefreshBackgroundTask because the data I send is very small and the entire auth process and save to the server only takes about 2 seconds. Below is a sample of the code I am using:

Code Block
func applicationWillResignActive() {
        print ("Should go to background now")
        beginUploadTask()
    }
func applicationDidEnterBackground() {
        print ("App in background now")
        WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date(timeIntervalSinceNow: 120), userInfo: nil) { (error: Error?) in
            if let error = error {
                print("Error occured while scheduling background refresh: \(error.localizedDescription)")
            } else {
                print ("Background refeesh scheduled")
            }
        }
    }
    func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
        for task in backgroundTasks {
            switch task {
            case let backgroundTask as WKApplicationRefreshBackgroundTask:
                beginUploadTask()
backgroundTask.setTaskCompletedWithSnapshot(false)
WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date(timeIntervalSinceNow: 30 * 60), userInfo: nil) { (error: Error?) in
                    if let error = error {
                        print("Error occured while scheduling background refresh: \(error.localizedDescription)")
                    } else {
                        print ("Background refeesh scheduled")
                    }
                }



Thanks in advance!

Replies

If your deployment target is set to watchOS7 for the minimum version your app may not be looking for your ExtensionDelegate any longer. You can use the property wrapper @WKExtensionDelegateAdaptor to observe your ExtensionDelegate. Make sure your ExtensionDelegate conforms to ObservableObject. Something to this effect should work.

Code Block
@main
struct RunPlanner: App {
@Environment(\.scenePhase) var scenePhase
@WKExtensionDelegateAdaptor(ExtensionDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { phase in
switch phase {
case .active:
print("\(#function) REPORTS - App change of scenePhase to ACTIVE")
case .inactive:
print("\(#function) REPORTS - App change of scenePhase Inactive")
case .background:
startBackgroundRefresh()
default:
print("\(#function) REPORTS - App change of scenePhase Default")
}
}
}