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

时间:2015-10-05 21:19:25

标签: swift ios9 watchkit health-kit

我在创建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)
}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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

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

或者我刚刚想到实现的东西......尝试在手机上捕捉商店的变化,然后通过后台传输将数据发送到手表。基本上你可以立即或每天更新...直到你,然后通过手机将它发送到手表,对我希望的后台应用程序来说不会是一个无赖。