Skip to content

Instantly share code, notes, and snippets.

@aokj4ck
Last active February 8, 2022 15:29
Show Gist options
  • Save aokj4ck/e4f048d4e2212fbf7e3d5d7dc299e5e0 to your computer and use it in GitHub Desktop.
Save aokj4ck/e4f048d4e2212fbf7e3d5d7dc299e5e0 to your computer and use it in GitHub Desktop.
// Construct startDate and endDate objects with NSDateComponents.
// See this article for more http://iaintheindie.com/2014/08/08/useful-nsdate-nscalendar-tricks/#StartEnd_of_the_Week
// For more examples of working with HealthKit see https://developer.apple.com/library/content/samplecode/Fit/Introduction/Intro.html
let workoutPredicate = NSPredicate(format: "(%K == %d) OR (%K == %d)",
HKPredicateKeyPathWorkoutType,
HKWorkoutActivityType.functionalStrengthTraining.rawValue,
HKPredicateKeyPathWorkoutType,
HKWorkoutActivityType.traditionalStrengthTraining.rawValue)
let timePredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])
let combinedPredicate = NSCompoundPredicate.init(andPredicateWithSubpredicates: [workoutPredicate, timePredicate])
let query = HKSampleQuery(sampleType: HKSampleType.workoutType(), predicate: combinedPredicate, limit: limitless, sortDescriptors: nil) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment