了解来自HKSourceQuery或一般来源的结果
我只是做了一个HKSourceQuery并得到了一些结果。 当我做了一个println
的结果,我得到这个: <HKSource:0x156c1520 "Health" (com.apple.Health)>//description of the object
如何使用这个来做一个使用HKQuery.predicateForObjectsFromSource(/* source goes here */)
的谓词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 alloc] init]
手动构buildHKSource
对象是不可能的。 在HealthKit框架中,Apple限制了大多数HK类使用init
的对象创build。 - 我相信你可以从使用
HKSource
属性(如name
和bundleIdentifier
设置的sources
find你的HKSource
对象。
这里是示例代码,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.source.bundleIdentifier = 'com.XXXX.XXXXX'"]; NSArray *tempResults = [[sources allObjects] filteredArrayUsingPredicate:predicate]; HKSource *targetedSource = [tempResults firstObject];