运行多个HealthKit样本查询的更好方法是什么?

时间:2022-02-25 21:29:21

I have a scenario where I need to retrieve multiple sets of data from HealthKit -- body temperature, weight, and blood pressure. I need all 3 before I can continue processing because they're going to end up in a PDF.

我有一个场景,我需要从HealthKit中检索多组数据 - 体温,体重和血压。在我可以继续处理之前,我需要全部3,因为它们最终将以PDF格式结束。

My naive first approach is going to be run one, then in the HKSampleQuery's resultsHandler call the second, then in that resultsHandler call the third. That feels kind of -- I don't know -- it feels like I'm missing something.

我的天真第一种方法是运行一种,然后在HKSampleQuery的resultsHandler中调用第二种方法,然后在结果中调用第三种方法。感觉有点 - 我不知道 - 感觉我错过了什么。

Is there a better way or is the naive approach reasonable?

是否有更好的方法或天真的方法是否合理?

3 个解决方案

#1


1  

You should try to run the queries in parallel for better performance. In the completion handler for each one, call a common function that notes a query has completed. In that common function, when you determine that all of the queries have finished then you can proceed to the next step.

您应该尝试并行运行查询以获得更好的性能。在每个的完成处理程序中,调用一个记录查询已完成的公共函数。在该常用功能中,当您确定所有查询都已完成时,您可以继续执行下一步。

One simple approach to tracking the completion of the queries in the common function is to use a counter, either counting up from zero to the number of queries, or down from the number of total queries to zero.

跟踪公共函数中查询完成的一种简单方法是使用计数器,从零开始向查询数量计数,或从总查询数量减少到零。

Since HealthKit query handlers are called on an anonymous background dispatch queue, make sure you synchronize access to your counter, either by protecting it with a lock or by modifying the counter on a serial dispatch queue that you control, such as the main queue.

由于在匿名后台调度队列上调用HealthKit查询处理程序,因此请确保同步对计数器的访问,方法是使用锁保护或通过修改您控制的串行调度队列(例如主队列)上的计数器。

#2


4  

I ran into this same problem, and a much better approach for any kind of nested async call would be to use GCD's dispatch groups. These allow you to wait until multiple async tasks have completed.

我遇到了同样的问题,对于任何类型的嵌套异步调用,更好的方法是使用GCD的调度组。这些允许您等到多个异步任务完成。

Here's a link with an example: Using dispatch groups to wait for multiple web services

这是一个示例链接:使用调度组等待多个Web服务

#3


2  

You're going to want to use GCD dispatch groups.

您将要使用GCD调度组。

First, set up a global variable for the main thread

首先,为主线程设置一个全局变量

var GlobalMainQueue: dispatch_queue_t {
  return dispatch_get_main_queue()
}

Next, create the dispatch group:

接下来,创建调度组:

let queryGroup = dispatch_group_create()

Right before your queries execute, call:

在查询执行之前,请致电:

dispatch_group_enter(queryGroup)

After your query executes, call:

查询执行后,调用:

dispatch_group_leave(queryGroup)

Then, handle your completion code:

然后,处理完成代码:

dispatch_group_notify(queryGroup, GlobalMainQueue) {
  // completion code here
}

#1


1  

You should try to run the queries in parallel for better performance. In the completion handler for each one, call a common function that notes a query has completed. In that common function, when you determine that all of the queries have finished then you can proceed to the next step.

您应该尝试并行运行查询以获得更好的性能。在每个的完成处理程序中,调用一个记录查询已完成的公共函数。在该常用功能中,当您确定所有查询都已完成时,您可以继续执行下一步。

One simple approach to tracking the completion of the queries in the common function is to use a counter, either counting up from zero to the number of queries, or down from the number of total queries to zero.

跟踪公共函数中查询完成的一种简单方法是使用计数器,从零开始向查询数量计数,或从总查询数量减少到零。

Since HealthKit query handlers are called on an anonymous background dispatch queue, make sure you synchronize access to your counter, either by protecting it with a lock or by modifying the counter on a serial dispatch queue that you control, such as the main queue.

由于在匿名后台调度队列上调用HealthKit查询处理程序,因此请确保同步对计数器的访问,方法是使用锁保护或通过修改您控制的串行调度队列(例如主队列)上的计数器。

#2


4  

I ran into this same problem, and a much better approach for any kind of nested async call would be to use GCD's dispatch groups. These allow you to wait until multiple async tasks have completed.

我遇到了同样的问题,对于任何类型的嵌套异步调用,更好的方法是使用GCD的调度组。这些允许您等到多个异步任务完成。

Here's a link with an example: Using dispatch groups to wait for multiple web services

这是一个示例链接:使用调度组等待多个Web服务

#3


2  

You're going to want to use GCD dispatch groups.

您将要使用GCD调度组。

First, set up a global variable for the main thread

首先,为主线程设置一个全局变量

var GlobalMainQueue: dispatch_queue_t {
  return dispatch_get_main_queue()
}

Next, create the dispatch group:

接下来,创建调度组:

let queryGroup = dispatch_group_create()

Right before your queries execute, call:

在查询执行之前,请致电:

dispatch_group_enter(queryGroup)

After your query executes, call:

查询执行后,调用:

dispatch_group_leave(queryGroup)

Then, handle your completion code:

然后,处理完成代码:

dispatch_group_notify(queryGroup, GlobalMainQueue) {
  // completion code here
}