Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从HealthKit检索心率数据时遇到问题_Ios_Objective C_Xcode_Healthkit_Hksamplequery - Fatal编程技术网

Ios 从HealthKit检索心率数据时遇到问题

Ios 从HealthKit检索心率数据时遇到问题,ios,objective-c,xcode,healthkit,hksamplequery,Ios,Objective C,Xcode,Healthkit,Hksamplequery,目前,我的健康应用程序中存储了今年1月初的两份心率记录。在我正在开发的当前应用程序中,我可以读取和写入心率数据 在我的视图中,我试图读取心率记录。但是,我无法检索任何数据 以下是我读取的心率数据代码: + (void)readHeartRateWithCompletion:(void (^)(NSArray *results, NSError *error))completion { HKQuantityType *heartRateType = [HKObjectType quant

目前,我的健康应用程序中存储了今年1月初的两份心率记录。在我正在开发的当前应用程序中,我可以读取和写入心率数据

在我的视图中,我试图读取心率记录。但是,我无法检索任何数据

以下是我读取的心率数据代码:

+ (void)readHeartRateWithCompletion:(void (^)(NSArray *results, NSError *error))completion {

    HKQuantityType *heartRateType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

    NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierStartDate ascending:YES];

    NSDate *lastSample = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastTsUploaded"];
    if (!lastSample) {
        lastSample = [NSDate dateWithTimeIntervalSinceNow:-24 * 60 * 60];
    }

    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDate *now = [NSDate date];
    NSCalendar *gregorian = [NSCalendar currentCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:now];
    [comps setYear:[comps year] - 2];
    NSDate *hundredYearsAgo = [gregorian dateFromComponents:comps];

    NSPredicate *pred = [HKQuery predicateForSamplesWithStartDate:hundredYearsAgo endDate:[NSDate date] options:HKQueryOptionStrictStartDate];

    HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:heartRateType predicate:pred limit:10 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
        if (!results) {
            NSLog(@"There are no heart rate results. The error was: %@.", error);
            return;
        }
    }];

    [[dcsContext sharedContext].healthStore executeQuery:sampleQuery];
}

为什么我的查询没有返回任何数据?

我花了好几个小时才弄明白为什么没有执行任何查询。我必须在所有代码之前添加以下内容:

if ([HKHealthStore isHealthDataAvailable]) {

self.healthStore = [[HKHealthStore alloc] init];

// Rest of my code below

//get the heart rates
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:now];
...

我还将我的[[dcs sharedContext].healthStore]替换为另一个healthStore实例,并将其作为属性添加到我的VC中。按照苹果公司的建议,我稍后会将其转换为单例,但现在这个修复程序可以正常工作。

我花了好几个小时才弄明白为什么没有执行我的查询。我必须在所有代码之前添加以下内容:

if ([HKHealthStore isHealthDataAvailable]) {

self.healthStore = [[HKHealthStore alloc] init];

// Rest of my code below

//get the heart rates
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:now];
...
我还将我的[[dcs sharedContext].healthStore]替换为另一个healthStore实例,并将其作为属性添加到我的VC中。稍后我将按照苹果的建议将其转换为单例,但目前这个修复程序仍然有效