我刚刚做了一个 HKSourceQuery 并得到了一些结果。当我做一个println
结果时,我得到了这个:<HKSource:0x156c1520 "Health" (com.apple.Health)>//description of the object
我如何使用它来使用HKQuery.predicateForObjectsFromSource(/* source goes here */)
我刚刚做了一个 HKSourceQuery 并得到了一些结果。当我做一个println
结果时,我得到了这个:<HKSource:0x156c1520 "Health" (com.apple.Health)>//description of the object
我如何使用它来使用HKQuery.predicateForObjectsFromSource(/* source goes here */)
这是 Obj-c 中的示例代码,
NSSortDescriptor *timeSortDesriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
HKQuantityType *quantityType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
HKSourceQuery *sourceQuery = [[HKSourceQuery alloc] initWithSampleType:quantityType samplePredicate:nil completionHandler:^(HKSourceQuery *query, NSSet *sources, NSError *error) {
//Here, sources is a set of all the HKSource objects available for "quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned"
HKSource *targetedSource = [[sources allObjects] firstObject];//Assume this as your targeted source
if(targetedSource)
{
NSPredicate *sourcePredicate = [HKQuery predicateForObjectsFromSource:targetedSource];
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:quantityType predicate:sourcePredicate limit:HKObjectQueryNoLimit sortDescriptors:[NSArray arrayWithObject:timeSortDesriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
//results array contains the HKSampleSample objects, whose source is "targetedSource".
}];
[self.healthStore executeQuery:query];
}
}];
[self.healthStore executeQuery:sourceQuery];
更新 1:
HKSource
使用手动构造对象[HKSource alloc] init]
。init
在 HealthKit 框架中,Apple 限制了大多数 HK 类的对象创建。HKSource
从sources
集合中找到您的对象。HKSource
name
bundleIdentifier
这是示例代码,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.source.bundleIdentifier = 'com.XXXX.XXXXX'"];
NSArray *tempResults = [[sources allObjects] filteredArrayUsingPredicate:predicate];
HKSource *targetedSource = [tempResults firstObject];