how to keep dataTask communication in background mode

I am working on some study in sending the data from iphone to server, and I want to keep the communication even when my program is working on the background mode.

For the details, when my program monitors the beacon' signal with special UUID by Corelocation( in other words, the iphone is taken into the beacon'region), then in Corelocation's handling function, the dataTask of URLSession will be excuted to forward the BLE signal's information to server.

Because the beacon sends the BLE signal periodically and continuously, I think the data will be sent from iphone to sever with my program continuously.

But now, my experiment result is, when I change the application into background:

-sometimes, the dataTask can be kept for several hours or one or two days without any problem

-sometimes, the dataTask will be stopped and after several minutes, it will be restarted automatically (really confusing). And in this case, I find the BLE monitoring program is kept to work, only the dataTask communication has been stopped

  1. I want to know the reason and the dataTask's action condition of the above phenomenon such as keeping or stoping or restarting the communication.

  2. Is there any method to keep the communication between the iphone and server without any interruption?

Thanks a lot!

Answered by DTS Engineer in 714858022

See my response here.

Share and Enjoy

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

Accepted Answer

See my response here.

Share and Enjoy

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

Dear Eskimo

Thanks for your reply very much!

I get a lot of very important informations from your reply as following:

Even if you use a background task to continue long transfers, it's still important that your app support resumable transfers. There are numerous situations where this might be useful:

if it's running on a device that doesn't support multitasking

if the network connection is interrupted during the transfer

if your app asks to start a background task and the system refuses to do so (that is, if -[UIApplication beginBackgroundTaskWithExpirationHandler:] returns UIBackgroundTaskInvalid)

if system resources get low and thus the system must suspend or terminate your app before its background tasks are complete

if the transfer takes more time than the background task will allow

Return back to my question, according to the information above, can I think the URLSession can not be kept for ever because of some reasons such as "if your app asks to start a background task and the system refuses to do so", or "if system resources get low and thus the system must suspend or terminate your app before its background tasks are complete" and so on ?

  • Could you please tell me why "the system refuses to do so" ?

  • What is the detail conditions of "if system resources get low" , which means low battery, low memory or some other reason?

  • In my experiment, the URLSession communication will be stopped suddenly and restarted suddenly, could you please tell me how long the URLSession communication will take from stop to start normally?

Best Regards,

Your specific questions don’t have answers. Well, the answer is that we don’t document the specific conditions under which the system will or won’t start background activities. That’s because the algorithm used to determine this is an implementation detail that changes from release to release.

I think you missed the key point of the post I referenced, namely this:

If you’re uploading small amounts of data and you want low latency, you can prevent your application from suspending and use a standard session. … Or you can use a hybrid approach, where you first try a standard session and then revert to a background session if you run out of time.

If latency is important and you’re transferring a small amount of data, don’t use a background session. Or rather, don’t start off using a background session. Rather, use a UIApplication background task to keep your process away for a short period of time and use that time to attempt an upload using a standard session.

Share and Enjoy

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

Dear Eskimo

Thank you very much!

About the key point you mentioned above, you suggested me to use a standard session to upload a small amounts of data. But Instead of sending data only once, my system is hoped to send data in the background periodically and contineously.

Do you think my system can work well without background session or using the hybrid approach?

Best Regard

But Instead of sending data only once, my system is hoped to send data in the background periodically and contineously.

Right. But presumably something is causing your app to run in the background in order to generate that data. That mechanism will give you background execution time to run your networking. Or maybe it won’t. The exact details of the execution time given to you by Core Bluetooth are not something I’m familiar with.

Share and Enjoy

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

Dear Eskimo

Thanks a lot!

As I introduced at the beginning, my program receives the Beacon's signal every second as a trigger to call Corelocation's handler, in which the URLSession (dataTask) is used to forward data received from Beacon to sever.

And for each communication with server, only several bytes data ( small amount of data) is sent to server.

The prcessing from receiving the Beacon's signal to Call Corelocation's handler seems no problem and can be kept foever in background.

And my problem is that URLSession (dataTask) is usually stopped and restarted suddenly in background, although only small amount of data is sent each time, which is send periodly and not sent without intervals.

 That mechanism will give you background execution time to run your networking. Or maybe it won’t.

You means is sometimes my program(URLSession communication) can not be excuted for some reason(some mechanism in iOS)?

Best Regards

And my problem is that URLSession (dataTask) is usually stopped and restarted suddenly in background

Are you using a UIApplication background task to keep your app running while the request is in flight?

Share and Enjoy

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

Dear Eskimo

Thanks for your reply very much!

Are you using a UIApplication background task to keep your app running while the request is in flight?

The "UIApplication background task" you wrote above meant the API such as "UIApplication.shared.beginBackgroundTask()"?

My program is shown in following:

let config:URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "abcd")
self.session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let task: URLSessionDataTask = session!.dataTask(with: myRequest as URLRequest)
task.resume()

Hope to continue to get your help!

The "UIApplication background task" you wrote above meant the API such as "UIApplication.shared.beginBackgroundTask()"?

Yes. See my UIApplication Background Task Notes post for a whole bunch o’ backstory on that.

My program is shown in following:

OK. That’s running your task is a background session and my advice is that you explore running in a standard session.

Share and Enjoy

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

how to keep dataTask communication in background mode
 
 
Q