Sending data to server in background mode(killed app state)

Hi guys, I got one issue about sending data with interval 1000 seconds to the server, while the app has been killed by user. I found the relevant document about app in background mode. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html but I checked the document, it seems there is no way to use NSTimer inside the block of beginBackgroundTaskWithName... function. NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(AppDelegate.handleTimer), userInfo: nil, repeats: true) Question: How to consistently send data to server with interval time while app has been terminated? Thank you, e

Replies

How to consistently send data to server with interval time while app has been terminated?

You don’t. There are two parts to this:

  • If the user removes your app from the multitasking UI, the system interprets that as a strong signal that the user does not want your app running in the background [1]. That state persists until the user next runs your app manually.

  • Even if you ignore the above, iOS does not provide a general way for apps to run periodically in the background. There are specific mechanism that do allow you to run in the background, but no general purpose way to get background execution time every N minutes.

… sending data with interval 1000 seconds to the server …

Just to be clear, you are not going to be able to accomplish this task. You will have to rethink your approach.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] The system may actually run your app in the background in this state under specific circumstances, but this general rule applies.

Hi Eskimo


Thank you for the quick explanation on my questions for sending generic data to the server in a consistent pace while the app has been terminated. I read through the document on background modes for apps for the link I attached above.


From my understanding of your explanation, beginBackgroundTaskWithName , WithExpirationTime function won't work in actually background mode but instead in a short period of moment when the user killed the app. And it also confused me a little bit on the approach of dealling with general data, it documented as "Background fetch".


The app regularly downloads and processes small amounts of content from the network.

It seems the fetch method would get the signal from system and wait to be awaken while user next time runs the app. -> no way to execute the fetch on the background(app has been killed), let alone in a regular(periodly) time.


Considering special cases such as bluetooth and corelocation data, bluetooth for example, Uses Bluetooth LE accessories bluetooth-central

The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the Core Bluetooth framework. (https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html)


I believe it could be accomplished in corebluetooth framework to send the data in background mode on regular basis. If you have relevent latest sample demo code regarding my issue, would be appreciated if you could attach the link here.


Warm regards,

E

Backgrounding and killing an app are two different things. Backgrounding an app does not kill the app if the app is configured to run in the background.


As to using BLE to wake the app, a connected BLE device would be needed to wake the app on a regular basis.


For power saving reasons, iOS does not support polling like you want. You need to rethink your architecture. What are you really trying to do?

Thank you for correcting my understanding about the background mode. My team lead mislead me on the understanding of the background mode. Originally I thought background mode is staging on user pressed the home buttom, and it would be miracle that developer could still operating the code in the dead ios app. But since the android developer implemented that mode and named it background mode. Therefore, my lead the background mode is the stage that app has been killed, and I need to sending data at interval time during that stage.


Based on your statement, I think it makes much sense. Then I assume, all the apple documents that indicate the Background Mode is actually the stage that the user pressed the home buttom.


Thanks again for your time.

  • Hi guys, I have a very similar need where I want to perform a background task (lets say every day at certain time) to get the latest online data to my offline store (data also includes images). Based on this thread, it seems like background task do not work if the app is terminated or killed. Since this article is 6 years older, I was wondering now if it is possible to run the background app when the app is killed or terminated?

Add a Comment

Hello. I have a similar need where I want to reload data (in my offline store) including media using background task. Based on this article, it seems like the process will not work when the app is killed. Since this thread is 6 year old, I am wondering if it is possible now? Also, is there any other way to achieve this?

I am wondering if it is possible now?

No. This is essentially a user experience thing. If the user removes your app from the multitasking UI, they are explicitly signalling to the system that they don’t want it to run.

Also, is there any other way to achieve this?

There are new ways to run code in the background, but all of them honour the above-mention user signal.

ps For a bunch of links to the latest docs and so on see the Background Tasks Resources pinned post.

Share and Enjoy

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

We are using the background task facility to work around another limitation of iOS, that does not exist on Android: scheduling periodic local notifications. iOS does not support abitrary intervals like "every 3 days" for local notifications. The idea was to use a background task to schedule all next days events as notifications. But if the user closes the app then our schedule task is not executed anymore, leading to certain notifications not being scheduled correctly.

iOS does not support abitrary intervals like "every 3 days" for local notifications.

Indeed. I’ve seen similar feedback from other folks. I strongly recommend that you file an enhancement request against the User Notifications framework for more flexible triggers.

Please post your bug number, just for the record.

Share and Enjoy

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