Healthkit enableBackgroundDeliveryForType不可用,无法每小时计算心率

Healthkit enableBackgroundDeliveryForType is unavailable, unable to calculate heart rate every hour

我试图在创建 HKObserverQuery 后触发 enableBackgroundDeliveryForType ,但我意识到甚至该方法本身也被禁用了。

/*!
 @method        enableBackgroundDeliveryForType:frequency:withCompletion:
 @abstract      This method enables activation of your app when data of the type is recorded at the cadence specified.
 @discussion    When an app has subscribed to a certain data type it will get activated at the cadence that is specified
                with the frequency parameter. The app is still responsible for creating an HKObserverQuery to know which
                data types have been updated and the corresponding fetch queries. Note that certain data types (such as
                HKQuantityTypeIdentifierStepCount) have a minimum frequency of HKUpdateFrequencyHourly. This is enforced
                transparently to the caller.
 */

那么如何触发每小时检查一次心率的工作。

 healthStore.enableBackgroundDeliveryForType(sampleType, frequency: .Immediate, withCompletion: {(succeeded: Bool, error: NSError?) in

        if succeeded{
            print("Enabled background delivery of weight changes")
        } else {
            if let theError = error{
                print("Failed to enable background delivery of weight changes. ")
                print("Error = \(theError)")
            }
        }
    })

以下是我的查询运行获取样本

        let sampleType =  HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!

        //let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)



        let query = HKObserverQuery(sampleType: sampleType, predicate: nil) {
            query, completionHandler, error in

            if error != nil {

                // Perform Proper Error Handling Here...
                print("*** An error occured while setting up the stepCount observer. ***")
                abort()
            }else{
                print("sampleType ",sampleType)
            }
            self.heightChangedHandler()
            completionHandler()
        }

func heightChangedHandler(){
    let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate,
                ascending: true)

            let sampleType =  HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
            let query = HKSampleQuery(sampleType: sampleType, predicate: nil, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]) { (query: HKSampleQuery, results: [HKSample]?, error: NSError?) -> Void in

                guard let results = results where results.count > 0 else {
                    print("Could not read the user's weight")
                    print("or no weight data was available")
                    return
                }

                for sample in results as! [HKQuantitySample]{

                    let heartRate = sample.quantity
                    let heartRateDouble = heartRate.doubleValueForUnit(self.countPerMinuteUnit)
                    print("Sample ",sample.quantity ," tyoe ",sample.quantityType," heartRateDouble ",heartRateDouble)

                }

            }

            healthStore.executeQuery(query)
}

无法安排 watchOS 2.0 应用在后台定期启动以查询 HealthKit 数据。当手表屏幕打开时,watchOS 2.0 的应用程序通常只能 运行 在前台。

2 件事,其中一个是您尝试访问的方法目前仅适用于 iPhone。 Apple Watch 甚至还没有背景状态,只有非活动状态。第二个苹果承诺手表应用程序扩展,而 运行 锻炼将允许留在前台,这通常是正确的,直到用户的手腕下降 :( 无论如何祝你好运,希望你想出如何实现你的目的..

我不熟悉这个过程,但你可以安排一个复杂功能每小时更新一次,你可以尝试从健康商店中提取最后一个已知的心率对象。无论如何我从来没有尝试过它,我几乎可以肯定有些东西需要改变,但只是一个想法。

或者我刚想实施的事情.. 尝试在 phone 上捕获对商店的更改,然后通过后台传输将数据发送到手表。基本上,您可以立即或每天进行更新。由您决定,然后从 phone 将其发送到手表,我希望这不会成为后台应用程序的麻烦。