klc*_*r89 5 objective-c uiapplicationdelegate ios healthkit hkobserverquery
我有一个正在尝试解决的问题,我已经设置了一个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];
}
Run Code Online (Sandbox Code Playgroud)
控制台输出,注意次数:仅从健康应用程序中删除一项时会出现这种情况,这是不正确的。
2014-12-29 16:50:20.121 TestApp[174:5674] Query
2014-12-29 16:50:20.124 TestApp[174:5674] Query
Run Code Online (Sandbox Code Playgroud)
我相信我现在已经通过设置 BOOL 标志来防止调用第二个伪造的 HKObserverQuery 并无缘无故地消除重复处理来解决该问题。代码:
- (void)observeHR
{
HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:[HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
predicate:nil
updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
{
if (!self.queryInProgress)
{
self.queryInProgress = YES;
if (!error)
{
[self queryWithCompletionHandler:completionHandler];
}
else
{
self.queryInProgress = NO;
if (completionHandler)
{
completionHandler();
}
}
}
else
{
NSLog(@"Query already in progress");
}
}];
[self.healthStore executeQuery:query];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1542 次 |
| 最近记录: |