首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向iPhone上的健康工具包添加饮食卡路里不会在watch应用程序健康工具包中更新

向iPhone上的健康工具包添加饮食卡路里不会在watch应用程序健康工具包中更新
EN

Stack Overflow用户
提问于 2015-11-19 19:49:46
回答 1查看 558关注 0票数 0

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

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

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

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

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

代码语言:javascript
复制
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)
}

有没有遇到过同样的问题,或者有什么需要解决的建议?

EN

回答 1

Stack Overflow用户

发布于 2015-11-20 07:00:27

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33803126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档