标题说明了一切,我一直在试图解决这个问题。从Watch:
中基于以前锻炼中已存储的数据最初希望使用HKStatisticsQuery
与HKStatisticsOptions.CumulativeSum
的组合,并且缺少的元素告诉查询获取每公里数据。
现在正在努力解决锻炼事件(暂停我的逻辑)
因此输出将是1km的距离和km所用的持续时间。 有任何改进的想法,请帮忙吗?
感谢艾伦的指导,我已经成功地分成了两个部分,一个抓住了锻炼:
print("HealthKit Workout:")
let healthStore:HKHealthStore = HKHealthStore()
let durationFormatter = NSDateComponentsFormatter()
var workouts = [HKWorkout]()
// Predicate to read only running workouts
let predicate = HKQuery.predicateForWorkoutsWithWorkoutActivityType(HKWorkoutActivityType.Running)
// Order the workouts by date
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
// Create the query
let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor])
{ (sampleQuery, results, error ) -> Void in
if let queryError = error {
print( "There was an error while reading the samples: \(queryError.localizedDescription)")
}
workouts = results as! [HKWorkout]
let target:Int = 0
print(workouts[target].workoutEvents)
print("Energy ", workouts[target].totalEnergyBurned)
print(durationFormatter.stringFromTimeInterval(workouts[target].duration))
print((workouts[target].totalDistance!.doubleValueForUnit(HKUnit.meterUnit())))
self.coolMan(workouts[target])
self.coolManStat(workouts[target])
}
// Execute the query
healthStore.executeQuery(sampleQuery)
其次是将样本处理成分裂:
let distanceType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
let workoutPredicate = HKQuery.predicateForObjectsFromWorkout(workout)
let startDateSort = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)
let query = HKSampleQuery(sampleType: distanceType!, predicate: workoutPredicate,
limit: 0, sortDescriptors: [startDateSort]) {
(sampleQuery, results, error) -> Void in
// Process the detailed samples...
if let distanceSamples = results as? [HKQuantitySample] {
var count = 0.00
var firstStart = distanceSamples[0].startDate
let durationFormatter = NSDateComponentsFormatter()
for (index, element) in distanceSamples.enumerate() {
count += element.quantity.doubleValueForUnit(HKUnit.meterUnit())
//print("Item \(index): \(element.quantity) - \(count) - \(element.startDate) \(element.endDate)")
if count > 1000 {
print("We reached a kilometer")
/* Print The Split Time Taken */
print(durationFormatter.stringFromTimeInterval(round((distanceSamples[index+1].endDate.timeIntervalSinceDate(firstStart)))))
firstStart = distanceSamples[index+1].endDate;
count = 0.00
}
}
答案 0 :(得分:1)
你需要按照顺序处理锻炼中的原始样本,并将它们自己总结在一起以找出分裂的位置,我想。我没有任何聪明的方法来撰写HKStatisticsQuery
或HKStatisticsCollectionQuery
,以某种方式为您工作。
答案 1 :(得分:0)
试试..
__block double walkingAVG = 0.0f;
HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];
// Create a predicate to set start/end date bounds of the query
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*-2] endDate:[NSDate date] options:HKQueryOptionStrictStartDate];
// Create a sort descriptor for sorting by start date
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKSampleQuery * sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType
predicate:predicate
limit:HKObjectQueryNoLimit
sortDescriptors:@[sortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
if(!error && results)
{
for(HKQuantitySample *samples in results)
{
walkingAVG += [[samples quantity] doubleValueForUnit:[HKUnit mileUnit]];
}
NSLog(@"Walking + Distance count::: %f km",walkingAVG);
compblock(walkingAVG);
}
}];
// Execute the query
[self.healthStore executeQuery:sampleQuery];