在背景上获取苹果健康数据

问题描述

我想在后台从苹果健康中获取数据。.我不知道在哪里调用我的函数updateOnBackground

我查看了相关主题,目前有:

 func updateOnBackground() {
        
        let quantityType = HKQuantityType.quantityType(forIdentifier: .heartRate)!
        self.healthStore.enableBackgroundDelivery(for: quantityType,frequency: .immediate) { (success,error) in
            if let error = error {
                print("\(error)")
            }
            if success {
                print("background delivery enabled")
            }
        }
        
        let query = HKObserverQuery(sampleType: quantityType,predicate: nil) { (query,completionHandler,error) in
            self.updateData(){
                completionHandler()
            }
        }
        healthStore.execute(query)
        
    }

这是在应用程序处于活动状态时获取数据的功能

 class func getMostRecentSample(for sampleType: HKSampleType,completion: @escaping (HKQuantitySample?,Error?) -> Swift.Void) {
        let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,end: Date(),options: .strictEndDate)
        let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,ascending: false)
        let limit = 1
        let sampleQuery = HKSampleQuery(sampleType: sampleType,predicate: mostRecentPredicate,limit: limit,sortDescriptors: [sortDescriptor]) { (query,samples,error) in
            dispatchQueue.main.async {
                guard let samples = samples,let mostRecentSample = samples.first as? HKQuantitySample else {
                        completion(nil,error)
                        
                        return
                }
                //print(samples)
                completion(mostRecentSample,nil)
            }
            
        }
        
        HKHealthStore().execute(sampleQuery)
    }
    func updateData(completionHandler: @escaping () -> Void) {
        let sampleType =  HKQuantityType.quantityType(forIdentifier: .heartRate)!
        HealthData.getMostRecentSample(for: sampleType) { (sample,error) in
            self.handleNewData(new: sample!)
            completionHandler()
        }
    }
    
    func handleNewData(new: HKQuantitySample) {
        print(new)
        
    }

我认为这里不需要函数getMostRecentSample,我还能做得更好吗?

解决方法

根据Apple文档https://developer.apple.com/documentation/healthkit/hkhealthstore/1614175-enablebackgrounddelivery的说法,为了观察查询和后台交付,没有比在

中的AppDelegate.swift内部更好的地方了。
func application(
    _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
    updateOnBackground()
    return true
}

这将确保您的应用会收到Apple Health发出的有关新数据已生成的通知。对于某些类型,更新频率可能会有所不同。例如,如果您尝试以.immediate频率获取步骤,则此操作将不起作用-只会在一个小时后发生(由Apple Health App自动确定)。

您可以尝试我的CocoaPod。这里是链接:https://cocoapods.org/pods/HealthKitReporter。它是HealthKit框架之上的包装,可简化读取/写入操作和观察。

您还可以在HKAnchoredObjectQuery实现中查看(也可以在窗格中)。就像观察者查询+示例查询一样,它也是一个长期运行的查询。在这里https://developer.apple.com/documentation/healthkit/hkanchoredobjectquery