首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HealthKit不最新

HealthKit不最新
EN

Stack Overflow用户
提问于 2015-01-23 00:07:08
回答 1查看 930关注 0票数 4

我在我的一个应用程序中使用HealthKit

我可能使用错了,但我发现在读取步骤时(我没有尝试使用其他数据),在查询中没有返回新的步骤数据,我需要打开Health应用程序,然后打开我的应用程序,以便查询返回的新信息。

代码语言:javascript
复制
if([HKHealthStore isHealthDataAvailable])
{
    HKHealthStore *healthStore = [[HKHealthStore alloc] init];

    //  Query for step data
    HKQuantityType *stepType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    NSSet *set = [NSSet setWithArray:@[stepType]];

    [healthStore requestAuthorizationToShareTypes:nil readTypes:set completion:^(BOOL success, NSError *error) {

        if(success)
        {
            //  Steps in 30 minute increments
            NSDateComponents *intervalComponents = [[NSDateComponents alloc] init];
            [intervalComponents setMinute:30];

            NSCalendar *calendar = [NSCalendar currentCalendar];

            NSDate *now = [NSDate date];

            NSDate *startDate = [calendar startOfDayForDate:now];
            NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];

            //  From the start of today until the end of today
            NSPredicate *datePredicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:(HKQueryOptionStrictStartDate | HKQueryOptionStrictEndDate)];

            HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:stepType
                                                                                   quantitySamplePredicate:datePredicate
                                                                                                   options:(HKStatisticsOptionCumulativeSum)
                                                                                                anchorDate:startDate
                                                                                        intervalComponents:intervalComponents];

            [query setInitialResultsHandler:^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *result, NSError *error) {

                for(HKStatistics *statistics in result.statistics)
                {
                    NSLog(@"%@, %@", @([statistics.sumQuantity doubleValueForUnit:[HKUnit countUnit]]), statistics.startDate);
                }
            }];

            [healthStore executeQuery:query];
        }
    }];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-12 03:56:19

我也发现了同样的东西-请看下面的答案

https://stackoverflow.com/a/27239738/3847

看起来HealthKit只是周期性地从CoreMotion导入step信息,因此出现了延迟。为我添加回调不起作用,因为它似乎只在步骤数据导入HealthKit后才会回调

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28100936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档