gpt4 book ai didi

ios - HealthKit 后台转换一分钟运行多次

转载 作者:行者123 更新时间:2023-11-29 11:47:14 29 4
gpt4 key购买 nike

我已经在委托(delegate)的 didFinishLaunching 方法中注册了四种类型的 HealthKit 数据的后台传送。数据类型为步数、 sleep 、锻炼和energyConsumed

我注意到我对这些数据类型的观察者查询在某些情况下每秒被 HealthKit/iOS 调用很多次。以下是调用 sleep 观察器查询的一些设备日志中的示例:

2017/03/27 07:21:58:821 Delegate - steps observer query received new data

2017/03/27 07:21:58:894 Delegate - steps observer query received new data

2017/03/27 07:21:58:936 Delegate - steps observer query received new data

2017/03/27 07:21:58:973 Delegate - steps observer query received new data

2017/03/27 07:21:58:993 Delegate - steps observer query received new data

2017/03/27 07:21:59:000 Delegate - steps observer query received new data

2017/03/27 07:21:59:024 Delegate - steps observer query received new data

2017/03/27 07:21:59:130 Delegate - steps observer query received new data

2017/03/27 07:21:59:145 Delegate - steps observer query received new data

2017/03/27 07:21:59:156 Delegate - steps observer query received new data

2017/03/27 07:21:59:169 Delegate - steps observer query received new data

2017/03/27 07:21:59:309 Delegate - steps observer query received new data

2017/03/27 07:21:59:328 Delegate - steps observer query received new data

2017/03/27 07:21:59:346 Delegate - steps observer query received new data

2017/03/27 07:21:59:404 Delegate - steps observer query received new data

2017/03/27 07:21:59:480 Delegate - steps observer query received new data

2017/03/27 07:21:59:499 Delegate - steps observer query received new data

2017/03/27 07:21:59:520 Delegate - steps observer query received new data

2017/03/27 07:21:59:547 Delegate - steps observer query received new data

2017/03/27 07:21:59:561 Delegate - steps observer query received new data

2017/03/27 07:21:59:571 Delegate - steps observer query received new data

2017/03/27 07:21:59:583 Delegate - steps observer query received new data

这是我的应用委托(delegate)中创建此日志的代码:

- (void)setUpStepsObserverQuery {

__weak typeof(self) weakSelf = self;

HKSampleType *sampleType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {

if (completionHandler) {
completionHandler();
}

if (error) {
DDLogDebug(@"App Delegate - An error occured while setting up the stepCount observer: %@", error.localizedDescription);

} else {
DDLogDebug(@"Delegate - steps observer query received new data");

weakSelf.activityTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[weakSelf endBackgroundTask:weakSelf.activityTask];
}];

// At this point I run an anchor query to check if there really is new data, and if there is I go ahead and send the new HealthKit data to our server.

}];

[_healthStore executeQuery:query]; // _healthStore is a shared instance of HKHealthStore I created earlier
}

这就是我为步数数据启用后台传送的方式。我在 didFinishLaunching 中调用了这个方法,它调用了我在上面分享的一个片段,setUpStepsObserverQuery

if ([defaults boolForKey:HK_ACTIVITY_SYNC]) {
[self setUpStepsObserverQuery];
[_healthStore enableBackgroundDeliveryForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]
frequency:HKUpdateFrequencyImmediate
withCompletion:^(BOOL success, NSError * _Nullable error) {
DDLogDebug(@"Delegate - enabled step count background updates");
}];

对于为什么 HealthKit 会在同一分钟内多次调用我的观察者查询有什么想法吗?

最佳答案

这是因为每次您的观察查询被调用时,您的 didFinishLaunching 函数也会被调用,并且 enableBackgroundDeliveryForType 也会被调用,每次启用时,查询都会被调用执行。

您必须检查是否启用了后台传送,不要再次启用它!

关于ios - HealthKit 后台转换一分钟运行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43038942/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com