使用 UUID 获取 HKWorkout 数据

Fetching HKWorkout data using UUID

我想知道我是否可以使用单个 HKWorkout 的 UUID 获取单个 HKWorkout 样本。当我尝试使用谓词比较锻炼 uuid 时出现错误。有什么想法吗?

func saveWorkout(workoutToSave: HKSample){
    let workoutPredicate = HKQuery.predicateForWorkouts(with: .running)
    let idPredicate = NSPredicate(format: "uuid == %@", workoutToSave.uuid.uuidString) //Gives error on runtime
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [workoutPredicate, idPredicate])
    let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: compoundPredicate, limit: 1, sortDescriptors: nil) { (sampleQuery, samplesFound, error) in
        if samplesFound != nil{
            print("Printing workout samples")
            print(samplesFound)
        }
    }
    database.healthStore.execute(sampleQuery)


}

我明白了。您需要做的就是创建一个特殊的 HKQuery 谓词。

let predicate = HKQuery.predicateForObject(with: UUID)