Is there a limit to the NSCompoundPredicate?
I'm having an issue with the NSCompoundPredicate(andPredicateWithSubpredicates: ) for an app that I'm working on. My current idea is that I want to retrieve the only the Traditional Strength Training and Functional Strength training workouts within a week's time period. Currently, I can get either the Traditional Strength Training workouts or Functional Strength Training workouts within the time period I have set to display. However, if I try to get both within the time period, nothing will display.
For example, here is where I can get the Traditional Strength training workouts to display within the time period.
func getWorkoutData() {
let date = Date()
let startDate = Calendar.current.dateInterval(of: .weekOfYear, for: date)?.start
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
let strengthPredicate = HKQuery.predicateForWorkoutActivities(workoutActivityType: .traditionalStrengthTraining)
let functionalStrengthPredicate = HKQuery.predicateForWorkoutActivities(workoutActivityType: .functionalStrengthTraining)
let traditionalStrengthTrainingPredicate = HKQuery.predicateForWorkouts(activityPredicate: strengthPredicate)
let functionalStrengthTrainingPredicate = HKQuery.predicateForWorkouts(activityPredicate: functionalStrengthPredicate)
let combinedPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [traditionalStrengthTrainingPredicate, datePredicate])
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let selectedWorkoutQuery = HKSampleQuery(
sampleType: HKWorkoutType.workoutType(),
predicate: combinedPredicate,
limit: HKObjectQueryNoLimit, sortDescriptors: [sortDescriptor]) { strengthQuery, samples, error in
guard let samples = samples else {
fatalError("An error has occurred \(error?.localizedDescription)")
}
guard let workouts = samples as? [HKWorkout] else { return }
DispatchQueue.main.async {
self.muscleStrength.append(contentsOf: workouts)
print(workouts)
}
}
self.healthStore?.execute(selectedWorkoutQuery)
}
But If I try to add Functional Strength Training workouts to the predicate nothing will display.
func getWorkoutData() {
let date = Date()
let startDate = Calendar.current.dateInterval(of: .weekOfYear, for: date)?.start
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
let strengthPredicate = HKQuery.predicateForWorkoutActivities(workoutActivityType: .traditionalStrengthTraining)
let functionalStrengthPredicate = HKQuery.predicateForWorkoutActivities(workoutActivityType: .functionalStrengthTraining)
let traditionalStrengthTrainingPredicate = HKQuery.predicateForWorkouts(activityPredicate: strengthPredicate)
let functionalStrengthTrainingPredicate = HKQuery.predicateForWorkouts(activityPredicate: functionalStrengthPredicate)
let combinedPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [traditionalStrengthTrainingPredicate, datePredicate, functionalStrengthTrainingPredicate])
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let selectedWorkoutQuery = HKSampleQuery(
sampleType: HKWorkoutType.workoutType(),
predicate: combinedPredicate,
limit: HKObjectQueryNoLimit, sortDescriptors: [sortDescriptor]) { strengthQuery, samples, error in
guard let samples = samples else {
fatalError("An error has occurred \(error?.localizedDescription)")
}
guard let workouts = samples as? [HKWorkout] else { return }
DispatchQueue.main.async {
self.muscleStrength.append(contentsOf: workouts)
print(workouts)
}
}
self.healthStore?.execute(selectedWorkoutQuery)
}
Is there a limit to the amount of predicates I can use in NSCompoundPreciate? If there is a better way to do this I am open.
Posted Mar 11, 2023, 1:45 AM UTC