1

我正在尝试为独立的 watchOS 8 应用程序实现 HealthKit 数据的后台交付。我正在关注如何从 HealthKit WWDC 演讲中获得最大收益,并且似乎已经添加了后台交付工作所需的一切,包括最近的 iOS 15 和 watchOS 8 com.apple.developer.healthkit.background-delivery权利。但是由于某种原因,在应用程序进入后台大约 3-5 小时后,后台交付停止工作。例如,我在晚上从应用程序接收更新,但在夜间更新停止传递,只有当我早上再次打开应用程序时我才会收到这些更新。请参阅ExtensionDelegate下面的代码

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    private let healthStore = HKHealthStore()
    private var anchor: HKQueryAnchor?

    func applicationDidFinishLaunching() {
        print("application did finish launching")

        activateHeathKit()
    }

    func activateHeathKit() {
        let types = Set([HKObjectType.categoryType(forIdentifier: .lowHeartRateEvent)!])

        healthStore.requestAuthorization(toShare: nil, read: types) { [weak self] success, _ in
            guard let `self` = self else {
                return
            }

            guard let lowHeartRateType = HKObjectType.categoryType(forIdentifier: .lowHeartRateEvent) else {
                return
            }

            `self`.healthStore.enableBackgroundDelivery(for: lowHeartRateType, frequency: .immediate) { success, _ in
                print("enableBackgroundDelivery: \(success) for lowHeartRateEvent")
            }

            let query = HKObserverQuery(sampleType: stepsType, predicate: nil) { _, completionHandler, error in
                `self`.updateLowHeartRate {
                    completionHandler()
                }
            }
            `self`.healthStore.execute(query)
        }
    }

    func updateLowHeartRate(completionHandler: @escaping () -> Void) {
        guard let lowHeartRateType = HKObjectType.categoryType(forIdentifier: .lowHeartRateEvent) else {return}

        let anchoredQuery = HKAnchoredObjectQuery(type: lowHeartRateType, predicate: nil, anchor:
                                                    self.anchor, limit: Int(HKObjectQueryNoLimit)) { [unowned self] query, newSamples,
            _, newAnchor, error -> Void in

            for item in newSamples ?? [] {
                let date = item.startDate
                let hour = Calendar.current.component(.hour, from: date)
                let minute = Calendar.current.component(.minute, from: date)

                let message = "Low heart rate from \(hour):\(String(format: "%02d", minute))"
                print(message)
            }

            self.anchor = newAnchor

            completionHandler()
        }

        healthStore.execute(anchoredQuery)
    }    
}
4

1 回答 1

1

我没有看到handle(_:)后台任务方法的实现,但也许只是没有显示。链接到此处的文档。

以防万一,这是我设置锻炼应用程序以更新表盘上的并发症的方式。

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    for task in backgroundTasks {
        if WKExtension.shared().applicationState == .background {
            if let watchComplication = task as? WKApplicationRefreshBackgroundTask {
                // do background work here
            }
        }
        task.setTaskCompletedWithSnapshot(false)
    }
    completePendingTasksIfNeeded()
}
于 2021-10-02T05:44:48.570 回答