当Healthkit在后台唤醒我的应用程序时,AppDelegate是否初始化? [英] Does the AppDelegate initialize when Healthkit wakes my app in the background?

查看:116
本文介绍了当Healthkit在后台唤醒我的应用程序时,AppDelegate是否初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码让我的应用在后台检测对HealthKit数据的更新.在后台运行此代码时,是否会调用AppDelegate的init方法? AppDelegate中的哪些方法将被调用?如果有人可以提供有关后台代码的应用程序生命周期的文档,将不胜感激!

I use the code below to have my app detect updates to HealthKit data in the background. Will the init method of my AppDelegate be called when this code is run in the background? What methods in the AppDelegate will be called? If someone can provide documentation about the application lifecycle of background code, that will be much appreciated!

[healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyHourly withCompletion:^(BOOL success, NSError *error) {
        if (success) {
            HKObserverQuery *observerQuery = [[HKObserverQuery alloc] initWithSampleType:type
                                                                               predicate:nil
                                                                           updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
                                                                               if (!error) {
                                                                                   [self retrieveHealthDataWithCompletionHandler:completionHandler];
                                                                               }
                                                                           }];
            [healthStore executeQuery:observerQuery];
        }

推荐答案

有点晚了,但希望对您或其他到达这里的人有所帮助.

A bit late but hopefully, it would still help you or anyone else who reach here..

在调用您的应用程序委托的application:didFinishLaunchingWithOptions:方法时,您可以假定该应用程序已启动.因此,Apple建议您在该方法中注册任何您希望包含的观察者查询.

When your app delegate’s application:didFinishLaunchingWithOptions: method is being called you can assume the app launches. Thats why Apple recommend you'll register any observer queries you'd like to have inside that method.

当将有您注册的类型的新数据时,HealthKit将唤醒您的应用程序. (到目前为止,您对任何新数据一无所知.)一旦您的应用程序启动完成,它将调用心爱的应用程序委托的application:didFinishLaunchingWithOptions:方法,如前所述,该方法应包含注册观察者查询的代码.

When there will be new data of the type you registered for, HealthKit will wake your app. (So far you still don't know anything about any new data.) Once your app finish its launching it will call the beloved app delegate’s application:didFinishLaunchingWithOptions: method, which as stated before, should contain the code of registering the observer queries.

注册查询后,接下来的事情将是获取有关新数据的更新(这是观察者查询的目的).

Once you register your queries, next thing will be getting an update about new data (this is the purpose of observer queries).

获取有关HealthKit中新内容的更新不包含数据本身.这就是为什么在观察者查询的updateHandler中应该启动另一个查询的原因-一种更具体的查询,它将获取所需的数据.

Getting the update about something new in HealthKit doesn't contain the data itself. Thats why in the updateHandler of the observer query you should initiate anther query - a more specific one that will fetch the wanted data.

就是这样.我将对您提供的代码进行一些更改,以使其正常工作:

That's it. I would make some changes in the code you provided in order for it to work:

[healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyHourly withCompletion:^(BOOL success, NSError *error) {
    if (success) {
      //Nothing much to do here
    }
}];

HKObserverQuery *observerQuery = [[HKObserverQuery alloc] initWithSampleType:type
                                                                           predicate:nil
                                                                       updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
    if (!error) {
       //Create and execute a query about the sample type.
       // Within the completion handler of the new query, don't forget to call completionHandler()
                
    }
}];
[healthStore executeQuery:observerQuery];

您可以找到更多详细信息此处:

You can find more details here:

接收后台交货

应用程序还可以通过调用HealthKit商店的enableBackgroundDeliveryForType:frequency:withCompletion:方法来注册以在后台接收更新.此方法注册您的应用以获取后台通知.只要将指定类型的新样本保存到商店,HealthKit就会唤醒您的应用程序.在您注册时指定的频率所定义的每个时间段内,您的应用最多只能调用一次.

Apps can also register to receive updates while in the background by calling the HealthKit store’s enableBackgroundDeliveryForType:frequency:withCompletion: method. This method registers your app for background notifications. HealthKit wakes your app whenever new samples of the specified type are saved to the store. Your app is called at most once per time period defined by the frequency you specified when registering.

应用启动后,HealthKit会为与新保存的数据匹配的任何观察者查询调用更新处理程序.如果您打算支持后台传送,请在应用程序委托的application:didFinishLaunchingWithOptions:方法中设置所有观察者查询.通过在application:didFinishLaunchingWithOptions:中设置查询,可以确保在HealthKit提供更新之前实例化查询并可以使用这些查询.

As soon as your app launches, HealthKit calls the update handler for any observer queries that match the newly saved data. If you plan on supporting background delivery, set up all your observer queries in your app delegate’s application:didFinishLaunchingWithOptions: method. By setting up the queries in application:didFinishLaunchingWithOptions:, you ensure that the queries are instantiated and ready to use before HealthKit delivers the updates.

在观察者查询完成对新数据的处理后,您必须调用更新的完成处理程序.这使HealthKit知道您已经成功接收了后台交付.

After your observer queries have finished processing the new data, you must call the update’s completion handler. This lets HealthKit know that you have successfully received the background delivery.

这篇关于当Healthkit在后台唤醒我的应用程序时,AppDelegate是否初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆