在我的当前项目中,我需要将HealthKit样本与我的应用程序同步。我正在从HealthKit获取样本数据,并将一些应用程序生成的样本写回到HealthKit。对于获取,我正在使用以下函数:
private func readHealthKitSample(sampleType:HKSampleType, limit: Int, startDate: NSDate, endDate: NSDate, completion: (([HKSample]?, NSError!) -> Void)!){
let mostRecentPredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate:endDate, options: .None)
// 2. Build the sort descriptor to return the samples in descending order
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
// 3. we want to limit the number of samples returned by the query to just 1 (the most recent)
let limit = limit
// 4. Build samples query
let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: limit, sortDescriptors: [sortDescriptor])
{ (sampleQuery, results, error ) -> Void in
if let error = error {
self.Logger.error("HealthKit Sample Data Fetch Error: \(error.localizedDescription)")
completion(nil , error)
return;
} else {
// self.Logger.debug("HealthKit Sample Data Fetch SUCCESS: \(results)")
}
// Execute the completion closure
if completion != nil {
completion(results,nil)
}
}
// 5. Execute the Query
self.healthKitStore.executeQuery(sampleQuery)
}
我的应用程序要求不考虑自己写入HealthKit Store的样本。因此,是否有一种方法可以以这样的方式过滤样本数据,以避免接收由我的应用程序编写的样本,并仅考虑由其他应用程序编写的样本?