0

我有一个带有 watch OS 2 应用程序的 iPhone 应用程序。

iPhone 和 watch 应用程序都可以成功地从健康包中写入和查询饮食卡路里信息。

如果我从手表上写下饮食卡路里信息,然后查询健康商店,我会得到正确的结果。

但是,如果我从 iPhone 应用程序写入饮食卡路里信息或在健康应用程序上手动输入,手表查询结果将永远不会获得新条目。

读取和写入健康包数据基于此处的 FIT 示例https://github.com/ooper-shlab/Fit-Swift/tree/master/Fit

func addFoodItem(calories: Double, completion: ((NSError?) -> Void)?){

    let nowDate: NSDate = NSDate()

    let energyQuantityConsumed: HKQuantity = HKQuantity(unit: HKUnit.kilocalorieUnit(), doubleValue: calories)
    let energyConsumedType: HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!
    let energyConsumedSample: HKQuantitySample = HKQuantitySample(type: energyConsumedType, quantity: energyQuantityConsumed, startDate: nowDate, endDate: nowDate)
    let energyConsumedSamples: Set<HKSample> = [energyConsumedSample]

    let foodType: HKCorrelationType = HKCorrelationType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierFood)!

    let foodCorrelation: HKCorrelation = HKCorrelation(type: foodType, startDate: nowDate, endDate: nowDate, objects: energyConsumedSamples)

    let completion: (Bool, NSError?) -> Void = {
        (success, error) -> Void in

        if completion != nil {
            completion!( error)
        }            
    }

    self.healthStore.saveObject(foodCorrelation, withCompletion: completion)
}

func fetchSumOfSamplesTodayForType(quantityType : HKQuantityType , unit: HKUnit, options: HKStatisticsOptions, forDay : Int, completion: ((Double, NSError?) -> Void)?) {

    let predicate = self.predicateForSamplesToday(forDay)

    let query: HKStatisticsQuery = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: options) {
        (_query, result, error) -> Void in


         let sum: HKQuantity? = result?.sumQuantity()

        if completion != nil {
            let value: Double = sum?.doubleValueForUnit(unit) ?? 0.0

            completion!(value, error)
        }
    }

    self.healthStore.executeQuery(query)
}

有没有遇到同样的问题或者有什么建议可以解决?

4

1 回答 1

0

手表上的 HealthKit 数据库仅包含源自手表的数据。它不包括来自配套手机的样本。

于 2015-11-19T23:00:26.503 回答