How to send a simple request when HKObserverQuery triggered in background?

Hi everyone,

I am trying to send a request to my server in my watch application when HKObserverQuery is triggered. This is working fine when my app is in foreground however the request is not sending when the app manually terminated or in background. HKObserverQuery works fine and triggered in these cases however the request is not sending. I researched about URLSessionConfiguration.background and background sessions but I could not figure it out. I don't want to download or upload a file, I just want to send a simple request when HKObserverQuery is triggered.

  • Can you show me to a path way to make it possible?
  • I am trying to test my watch app with my iPhone, I am assuming the behavior of these scenarios might be same in both device, am I correct?
let urlsession = URLSession(configuration: URLSessionConfiguration.background(withIdentifier: "enablement"), delegate: self, delegateQueue: nil)
        
        let dataTask = urlsession.dataTask(with: urlRequest)
        dataTask.resume()

As shown in the code snippet, I tried to set background configuration to my URLSession.

"This is working fine when my app is in foreground however the request is not sending when the app manually terminated or in background."

Yeah, this is as designed. A URL session that isn't configured as a background session, will be terminated if the app is suspended.

If you would like to do networking while your app is suspended, you will need a background URLSession, which allows the system to do networking for your app at its discretion.

Is there any reason that doesn't allow you to use a background URLSession?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

@DTS Engineer Hello, thank you for reply. No, there isn't a limitation or reason to use background URLSession. I am able to send requests with following code snippet while app is suspended:

let urlsession = URLSession(configuration: URLSessionConfiguration.background(withIdentifier: "enablement"), delegate: self, delegateQueue: nil)
        
let dataTask = urlsession.downloadTask(with: urlRequest)
dataTask.resume()

However, when I use this background urlSession with dataTask instead of downloadTask, it's doesn't work. To sum up, I couldn't find a task type which suits to my operation. I don't want a download, upload or time consuming operation. Using download task works but doesn't seem like the proper way to it. If this is wrong way to do, can you explain to me the proper way?

OK, this topic was discussed in how to keep dataTask communication in background mode.

Basically, if you need to transfer a small amount data with low latency on iOS, you can use a UIApplication background task to keep your process running for a short period of time, which hopefully allows you to finish the data transfer.

I noticed that your post was tagged with watchOS though. Are you on watchOS?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

How to send a simple request when HKObserverQuery triggered in background?
 
 
Q