Есть ли надежный способ продлить временную шкалу осложнений на AppleWatch, если приложение не запущено?

Я пытаюсь расширить свою временную шкалу усложнения из фона, но самое позднее после 1 или 2 продления временной шкалы она больше не работает, и содержимое усложнения больше не обновляется (у меня есть усложнение с новым значением каждый минута).

Приложение предназначено только для просмотра.

Код моего фонового обновления:

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")
            }
        }
    }
}

У вас есть идеи, почему фоновое обновление приложения не работает постоянно? Или может быть проблема в сложном методе обновления, .extendTimeline(for:?


person 3xypn0s    schedule 11.02.2020    source источник
comment
Подобный вопрос уже задавался, но я увидел его слишком поздно: stackoverflow.com/q/60106610/12523385   -  person 3xypn0s    schedule 12.02.2020