HealthKit Background Delivery and URLSession.shared.dataTask

Hello. I have implemented background delivery for detecting changes in health kit with HKObserverQuery. It works well, I am reading changes. And I am sending this changes to an https endpoint with using an URLSession.shared.dataTask inside the HKObserverQuery callback while my app is terminated. I have several questions about this:

  1. Is starting a URLSession.shared.dataTask inside HKObserverQuery callback when app is terminated is correct way to do it?
  2. I am calling HKObserverQuery completion handler whatever dataTask returned success or failure but I am wondering what if the network connection is low and this dataTask response could not received in 2-3 seconds. I have read background deliveries should take 1-2 seconds. Should I use an URL session with background configuration for sending those HTTPS requests? If so, should I use download task or upload task (they don't fit my requirements I am sending a simple json)?
Answered by DTS Engineer in 854096022

In a situation like this, where the system has resumed or relaunched your app in the background to tell it about a specific event, you have to decide whether you want to use a standard session or a background session. I talk more about this trade-off in my reply on this thread.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

In a situation like this, where the system has resumed or relaunched your app in the background to tell it about a specific event, you have to decide whether you want to use a standard session or a background session. I talk more about this trade-off in my reply on this thread.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I read that thread and associated threads and watched WWDC19. As I see, you answered and explained background task very detailed in these threads. Really appreciated. However, instead of answering my questions, they raised more questions for me :).

  1. You wrote that "The UIApplication background task mechanism allows you to prevent your app from being suspended for short periods of time". In my case, the application is terminated and relaunched by HKObserverQuery. At this point I fetch new health informations and use standart session to send these changes to my server. So should I execute this normal session with the UIApplication background task while app relaunched with HKBackgroundDelivery? I am asking this question because UIApplication background task looks like in the documentation , it should used when app is in foreground and app closed by the user while some tasks are not completed then we should use UIApplication background task to complete these tasks.

  2. I looked to BGAppRefreshTask and it looks really fit to my case. However, WWDC19 mention that "the system learns usage patterns of the application and starts launching your app according to this pattern and this also means that an app that isn't used frequently may not launch as frequently as well." But I want to send these health data changes when they are detected by HKObserverQuery. Since my app is designed for people with disabilities, they are unlikely to open the application frequently. This makes the usage-pattern–based scheduling of BGAppRefreshTask unsuitable for my case. Also in the same video mentioned that "Ensure files are accessible while device is locked". In my case, health data are encrypted by Health Kit while the device is locked and HKObserverQuery doesn't trigger while the device is locked.

  3. I tried learn about BGHealthResearchTask but there is no detailed explanation about it. When we should use it? How can I consider my work as a research? How is it schedule the task? What are the requirements to use it?

I also looked at BGAppProcessingTask . It says it should be used time-consuming and intensive work but my task is simple and doesn't require several minutes.

I tried to make my question as clear as possible. As I mentioned above, thank you so much for your detailed answers. Whenever I see your name in a thread, it makes me happy because you always provide clear and detailed explanations.

HealthKit Background Delivery and URLSession.shared.dataTask
 
 
Q