1

I'm trying to extend my complication timeline from background, but at the latest after 1 or 2 extension of the timeline it doesn't work anymore and the content of the complication is not updated anymore (I have a complication with a new value every minute).

The App is a Watch only App.

The code of my background refresh is:

import WatchKit

class ExtensionDelegate: NSObject, WKExtensionDelegate {

    let defaults = UserDefaults.standard

    func applicationDidFinishLaunching() {
        self.scheduleBackgroundRefresh()
    }

    func applicationDidBecomeActive() {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillResignActive() {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, etc.
    }

    func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
        // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
        for task in backgroundTasks {
            // Use a switch statement to check the task type
            switch task {
            case let backgroundTask as WKApplicationRefreshBackgroundTask:
                self.scheduleBackgroundRefresh()

                let server = CLKComplicationServer.sharedInstance()

                for complication in server.activeComplications ?? [] {
                    server.extendTimeline(for: complication)
                }
                backgroundTask.setTaskCompletedWithSnapshot(false)
            default:
                // make sure to complete unhandled task types
                task.setTaskCompletedWithSnapshot(false)
            }
        }
    }

}

extension ExtensionDelegate{
    private func scheduleBackgroundRefresh(){
        let fireDate = Date().addingTimeInterval(TimeInterval(60*60))
        let userInfo = ["reason" : "complication extendTimeline"] as NSDictionary
        WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: fireDate, userInfo: userInfo){
            (error) in
            if error == nil {
                NSLog("Background refresh scheduled")
            }
        }
    }
}

Do you have any idea, why the background app refresh is not working constantly? Or could be the problem in the complicatione update method, .extendTimeline(for:?

3xypn0s
  • 80
  • 6
  • A similar question has already been asked, but I saw it too late: https://stackoverflow.com/q/60106610/12523385 – 3xypn0s Feb 11 '20 at 21:28

0 Answers0