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

标签 swift ios9 watchkit healthkit

我试图在创建 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 的应用程序通常只能在 watch 屏幕打开时在前台运行。

关于swift - Healthkit enableBackgroundDeliveryForType不可用,无法每小时计算心率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32958068/

相关文章:

iOS9 分割 View 中的多任务处理 - 当另一个应用程序打开键盘时发出通知

iphone - iOS9 上的 UIAlertController ActionSheet 问题

swift - 如何构建路线并访问其坐标?

ios - 在 HealthKit 中请求授权 : why the result is always successful?

ios - Firebase 客户端 UID 和 Firebase 函数 UID 不匹配

swift - Realm 列表过滤器 Swift

arrays - swift:平均连续正数

ios - 日期在 iOS 9 的 FSCalendar 中不可见

ios - WatchConnectivity:尽管更新了上下文,但 didReceiveApplicationContext 永远不会被调用

Watchkit密码按钮按下检测