我有一个我正在尝试解决的问题,我已经设置了一个HKObserveryQuery
,它运行良好并为我收集新数据。
然而问题是,有时当我手动将项目添加到健康应用程序后返回健康应用程序并删除它时,我注意到HKObserverQuery
我已经设置了两次非常接近的火灾,我正在尝试解决,因为我使用这个观察者稍后上传一些数据,我不想重复。
我将不胜感激提供的任何帮助。代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setup];
return YES;
}
- (void)setup
{
if ([HKHealthStore isHealthDataAvailable])
{
self.healthStore = [[HKHealthStore alloc]init];
NSSet *readTypes = [NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]];
[self.healthStore requestAuthorizationToShareTypes:nil
readTypes:readTypes
completion:^(BOOL success, NSError *error)
{
if (!error && success)
{
[self observeHR];
[self.healthStore enableBackgroundDeliveryForType:
[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error){}];
}
}];
}
}
- (void)observeHR
{
HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:[HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
predicate:nil
updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
{
if (!error)
{
// Randomly called twice *VERY* close together
NSLog(@"Query");
[self queryWithCompletionHandler:completionHandler];
}
else
{
if (completionHandler)
{
completionHandler();
}
}
}];
[self.healthStore executeQuery:query];
}
控制台输出,注意时间:仅从健康应用程序中删除一项时会发生这种情况,这是不正确的。
2014-12-29 16:50:20.121 TestApp[174:5674] Query
2014-12-29 16:50:20.124 TestApp[174:5674] Query