首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >watchOS 8 HealtKit后台传送在后台运行几个小时后停止工作

watchOS 8 HealtKit后台传送在后台运行几个小时后停止工作
EN

Stack Overflow用户
提问于 2021-10-01 08:32:51
回答 1查看 135关注 0票数 1

我正在尝试为一个独立的watchOS 8应用程序实现HealthKit数据的后台交付。我一直在关注Gettings the most out of HealthKit WWDC talk,似乎已经添加了后台交付工作所需的所有内容,包括最近的iOS 15和watchOS 8 com.apple.developer.healthkit.background-delivery授权。但由于某些原因,后台交付在应用程序转到后台后大约3-5小时后停止工作。例如,我在晚上收到应用程序的更新,但一夜之间更新就停止了,只有当我早上再次打开应用程序时,我才会收到更新。请参阅下面的ExtensionDelegate代码

代码语言:javascript
复制
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)
    }    
}
EN

回答 1

Stack Overflow用户

发布于 2021-10-02 05:44:49

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

为了以防万一,这里是我如何设置我的锻炼应用程序,以更新手表表面的复杂性。

代码语言:javascript
复制
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()
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69402871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档