【问题标题】:watchOS 8 HealtKit background delivery stops working after a few hours in backgroundwatchOS 8 HealthKit 后台交付在后台几个小时后停止工作
【发布时间】:2021-11-22 22:10:50
【问题描述】:

我正在尝试为独立的 watchOS 8 应用实现 HealthKit 数据的后台交付。我正在关注Gettings the most out of 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)
    }    
}

【问题讨论】:

    标签: watchkit healthkit watchos-8


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多