了解HKSourceQuery或一般来源的结果 [英] Understanding results from HKSourceQuery, or Sources in general

查看:180
本文介绍了了解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:

  1. 无法使用[HKSource alloc] init]手动构造HKSource对象.在HealthKit框架中,Apple限制了大多数HK类使用init创建对象.
  2. 我相信您可以使用namebundleIdentifier之类的HKSource属性从sources集中找到HKSource对象.
  1. It is not possible to construct HKSource object manually using [HKSource alloc] init]. In HealthKit framework, Apple restricted creation of objects using init for most of the HK classes.
  2. I believe that you can find your HKSource object from the sources set using the HKSource properties like name and bundleIdentifier.

这是示例代码,

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.source.bundleIdentifier = 'com.XXXX.XXXXX'"];
    NSArray  *tempResults = [[sources allObjects] filteredArrayUsingPredicate:predicate];

    HKSource *targetedSource = [tempResults firstObject];

这篇关于了解HKSourceQuery或一般来源的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆