了解HKSourceQuery或一般来源的结果 [英] Understanding results from HKSourceQuery, or Sources in general
本文介绍了了解HKSourceQuery或一般来源的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚做了一个HKSourceQuery并得到了一些结果.当我对结果进行println
时,得到了:<HKSource:0x156c1520 "Health" (com.apple.Health)>//description of the object
I just did a HKSourceQuery and got some results. When I do a println
of the results, I got this: <HKSource:0x156c1520 "Health" (com.apple.Health)>//description of the object
如何使用HKQuery.predicateForObjectsFromSource(/* source goes here */)
推荐答案
以下是Obj-c中的示例代码,
Here is the sample code in 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]
手动构造HKSource
对象.在HealthKit框架中,Apple限制了大多数HK类使用init
创建对象. - 我相信您可以使用
name
和bundleIdentifier
之类的HKSource
属性从sources
集中找到HKSource
对象.
- It is not possible to construct
HKSource
object manually using[HKSource alloc] init]
. In HealthKit framework, Apple restricted creation of objects usinginit
for most of the HK classes. - I believe that you can find your
HKSource
object from thesources
set using theHKSource
properties likename
andbundleIdentifier
.
这是示例代码,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.source.bundleIdentifier = 'com.XXXX.XXXXX'"];
NSArray *tempResults = [[sources allObjects] filteredArrayUsingPredicate:predicate];
HKSource *targetedSource = [tempResults firstObject];
这篇关于了解HKSourceQuery或一般来源的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文