0

I want to fetch HealthKit quantity data and sort them by quantity value. Is it possible?

I know I can filter them by using HKPredicateKeyPathQuantityin the NSPredicate but I can't find similar key for sorting

Example

let type = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)
let sort = [
  NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true),
  NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: true),

  //I want one more Sort Item here :(
  //NSSortDescriptor(key: SortIdentifierIdeQuantity, ascending: true)
]

let predicate = NSPredicate(format: "\(HKPredicateKeyPathStartDate) > %@ AND \(HKPredicateKeyPathStartDate) < %@ AND \(HKPredicateKeyPathQuantity) == %@",
  argumentArray:[start, end, value])

let query = HKSampleQuery(sampleType: type, predicate: predicate, limit: limit, sortDescriptors: sort) { query, res, error in
  completion(res, error)
}

healthKit.executeQuery(query)

}
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
Kostiantyn Koval
  • 8,407
  • 1
  • 45
  • 58

1 Answers1

1

It is only possible to have HealthKit sort the results by date currently. You should file a bug with Apple for the enhancement that you'd like.

Allan
  • 7,039
  • 1
  • 16
  • 26