Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 香港健身总能量消耗量';t与HKQuantityTypeIdentifierActivityEnergyBurned关联_Ios_Swift_Healthkit - Fatal编程技术网

Ios 香港健身总能量消耗量';t与HKQuantityTypeIdentifierActivityEnergyBurned关联

Ios 香港健身总能量消耗量';t与HKQuantityTypeIdentifierActivityEnergyBurned关联,ios,swift,healthkit,Ios,Swift,Healthkit,这与其说是编程语法问题,不如说是数据结构问题。健康应用程序的数据结构有点像一个黑匣子 我想查询HKHealthStore并创建项目的每日摘要,包括ActiveEnergyBurned,以及包括totalEnergyBurned在内的训练摘要 我有成功检索此信息的代码(如下)。然而,每天的总训练次数通常少于当天的训练次数!我确信我的代码不是问题所在,因为苹果健康应用程序中显示的数字完全相同。例如: 昨天的训练: 我的应用程序 总能量消耗=905千卡 昨天消耗的有功能量之和为655千卡 健康应用程序

这与其说是编程语法问题,不如说是数据结构问题。健康应用程序的数据结构有点像一个黑匣子

我想查询HKHealthStore并创建项目的每日摘要,包括ActiveEnergyBurned,以及包括totalEnergyBurned在内的训练摘要

我有成功检索此信息的代码(如下)。然而,每天的总训练次数通常少于当天的训练次数!我确信我的代码不是问题所在,因为苹果健康应用程序中显示的数字完全相同。例如:

昨天的训练: 我的应用程序 总能量消耗=905千卡 昨天消耗的有功能量之和为655千卡 健康应用程序显示的数字完全相同

如果ActiveEnergyBurned不包括锻炼,它包括什么?我没有再燃烧655的能量。在我看来,ActiveEnergyBurned不包括锻炼是不可能的

   //to get sum of day's activeCaloriesBurned:

func getActiveCalories(startDate:NSDate, endDate:NSDate){
    let sampleType =      HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)
    let hkUnit = HKUnit.kilocalorieUnit()

    getSumStatsFor(sampleType, hkUnit: hkUnit, startDate: startDate, endDate: endDate) { (hdObject, result) -> Void in
        hdObject.activeCalories = result
    }
}

func getTotalsForDataType(quantitiyType:HKQuantityType, startDate:NSDate,      endDate:NSDate, completion:(HKStatisticsCollection!, NSError!) -> Void){
    println("getTotalsForDataType start: \(startDate) end: \(endDate)")
    let dayStart = NSCalendar.currentCalendar().startOfDayForDate(startDate)
    let addDay = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: 1, toDate: endDate, options:nil)
    let dayEnd = NSCalendar.currentCalendar().startOfDayForDate(addDay!)  //add one day
    let interval = NSDateComponents()
    interval.day = 1

    let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: HKQueryOptions.None)
    let newQuery = HKStatisticsCollectionQuery(quantityType: quantitiyType,
                                    quantitySamplePredicate: predicate,
                                                    options: HKStatisticsOptions.CumulativeSum,
                                                 anchorDate: dayStart,
                                         intervalComponents: interval)
    newQuery.initialResultsHandler = {
        query, results, error in
        if error != nil {
            println("*** An error occurred while calculating the statistics: \(error.localizedDescription) ***")
            completion(nil, error)
        }else{
            completion(results,error)
        }
    }
  self.healthKitStore.executeQuery(newQuery)
}

//to get workout totalCalories burned

func readWorkouts(completion: (([AnyObject]!, NSError!) ->Void)!){
        let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
    let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: nil, limit: 0, sortDescriptors: [sortDescriptor]) { (sampleQuery, results, error) -> Void in
            if let queryError = error {
            println( "There was an error while reading the samples: \(queryError.localizedDescription)")
        }
        completion(results,error)
    }
    healthKitStore.executeQuery(sampleQuery)
}

这是因为HealthKit训练API的设计存在缺陷。使用当前的API,第三方应用程序可以创建
HKWorkout
s,其中
totalEnergyBurned
大于0,但不会创建
hkQuantityTypeIdentifierActivienergyburned
类型的相关
HKSamples
。例如,将训练数据输入HealthKit的第三方应用程序可以做到:

HKHealthStore *healthStore = [HKHealthStore new];
HKWorkout *workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypePlay
                                               startDate:[NSDate date]
                                                 endDate:[NSDate date]
                                                duration:100.0
                                       totalEnergyBurned:[HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit] doubleValue:445]
                                           totalDistance:[HKQuantity quantityWithUnit:[HKUnit meterUnit] doubleValue:1000]
                                                metadata:nil];
[healthStore saveObject:workout withCompletion:nil];
请注意,没有创建任何类型为
hkQuantityTypeIdentifierActiviteEnergyBurned
HKSamples
。然后,当你总结一天消耗的活动能量,并将其与训练消耗的总能量进行比较时,你将得到0千卡对445千卡。创建训练后,一个好的第三方应用程序可以做到以下几点:

NSArray *workoutSamples = @[[HKQuantitySample quantitySampleWithType:HKQuantityTypeIdentifierActiveEnergyBurned
                                                            quantity:[HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit] doubleValue:workout.totalEnergyBurned]
                                                           startDate:workout.startDate
                                                             endDate:workout.endDate]];
[healthStore addSamples:workoutSamples toWorkout:workout completion:nil];
这样的话,至少只有有效能燃烧的样本。现在你将得到445千卡对445千卡

在我对第三方应用程序的测试中,我发现大多数应用程序确实添加了活动能量燃烧样本,但有些应用程序(如Nike Running)没有添加


一个棘手的解决办法是提取训练的所有活动能量样本(你必须使用
startDate
endDate
,因为
predicateRobjectsFromWorkout
具有与上述类似的问题),如果没有任何样本,假设该源没有为该锻炼创建活动能量样本,并将锻炼的<>代码> TooPosiyWiels添加到一天的活动能量消耗总和中。

@ MFLAC,你应该考虑对HealthKit进行一个雷达(),因为这使得HealthKIT数据不可用。请随意愚弄我的:rdar://21584885That 这真是一只虫子!我上面提到的训练是使用Runtastic应用程序。有趣的是,有时即使使用Apple Watch训练应用程序,也会出现这种情况,尽管有时(第二天!)数据会神秘地出现在activeEnergy中!我无法想象这是怎么发生的。我也会申请雷达,我申请了rdar://21856618Isn‘t总燃烧能量=燃烧的有功能量+静止能量?