我有一个iPhone应用程序和一个附带的watch OS 2应用程序。
iPhone和手表应用程序都可以成功地写入和查询健康工具包中的饮食卡路里信息。
如果我写下手表上的饮食卡路里信息,然后查询健康商店,我会得到正确的结果。
但是,如果我从iPhone应用程序中写入饮食卡路里信息,或者在健康应用程序上手动输入,手表查询结果永远不会获得新的条目。
读取和写入健康工具包数据基于FIT示例here 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)
}有没有遇到过同样的问题,或者有什么需要解决的建议?
发布于 2015-11-20 07:00:27
手表上的HealthKit数据库只包含源自手表的数据。它不包括配套手机的样本。
https://stackoverflow.com/questions/33803126
复制相似问题