24

I have to upload an array of image files to database, therefore, I stumbled upon Apple's background execution guide to make sure the app still uploads the data when user suspends or terminates my app.

But in the desciption, it says giving it a little extra time to finish its work if we call beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: to start a background task.

How long is little extra time precisely?

donkey
  • 4,285
  • 7
  • 42
  • 68
  • If you are transferring data then the best approach would be to use `NSURLSession` as described in the iOS programming guide – Paulw11 Feb 02 '15 at 11:35
  • I am not downloading data, but uploading some images using `parse`. I am not sure NSURLSession is the most efficient approach – donkey Feb 02 '15 at 12:15

5 Answers5

21

Correct me if I am wrong, but I have stumbled upon a perfect article from Xamarin that discusses iOS backgrounding feature.

I will simply break down to two parts, ios pre 7 and ios 7+:

iOS version pre 7

The answer is simply 600 seconds (10 minutes), reason is provided by the article above.

iOS version 7+

The answer is that the time system allocates you is opportunistic. You will have to use @Gary Riches's suggestion

NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]); to find out. The reason for it being opportunistic is the way iOS 7+ handles background tasks is completely different, certainly optimised. To be exact, It has an intermittent behaviour, and therefore, if you need background tasks such as downloading a big chuck of data, it will be much more effective if you use `NSURLSession` instead.

However, in my special case, I am uploading one single object that contains one file to be exact. I do not have to consider NSURLSession for uploading a small amount of data. And besides, it's uploading task, it can take as much time as it wants. :-)

For these TL;DR visitors, the answer above should be sufficient. For more details, please refer to the article above.

Michal Šrůtek
  • 1,647
  • 16
  • 17
donkey
  • 4,285
  • 7
  • 42
  • 68
  • 1
    Im getting background task remaning time as 0. but still mu method is calling? – siva krishna Mar 21 '17 at 12:04
  • 2
    This can happen when the app is built in a debug configuration. When built for release and distributed, the OS is much more militant about stopping background tasks. – user3847320 Aug 10 '17 at 14:26
10

The amount of time will differ based on many different variables, but the value can be checked by referencing the backgroundTimeRemaining property on UIApplication:

NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Gary Riches
  • 2,847
  • 1
  • 22
  • 19
8

If you want to upload your files when app is in background, you should use Apple's background service. iOS will give your app time of approx. 3 minutes (based on some experience) for completing your task and then it will kill your app.

Apple allows longer run of the app in special cases. For that you will need to use UIBackgroundModes in your info.plist file. For more info on these special cases see table 3-1 on this link.

Here is a nice article that describes background task run time and how to achieve long running background task in iOS.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
mitesh
  • 136
  • 5
  • Thanks, I think UIBackgroundModes is if you need special services to run in the background as the list suggests. For the minutes tho, may you cite where you get your 3 minutes from? Much appreciated – donkey Feb 02 '15 at 14:26
  • @oasisweng check this [link](http://hayageek.com/ios-long-running-background-task/). – mitesh Feb 02 '15 at 14:40
  • Thanks, I have stumbled a good article I find it worth sharing. [link](http://developer.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/ios_backgrounding_with_tasks/) The author of your link does not give evidence to why its 3 minutes. In fact, in ios7, the time provided is opportunistic. See the link I give you. – donkey Feb 02 '15 at 17:30
  • Nice article. Thanks for sharing. Actually author of the link I share has already answered why its 3 minutes. He has given a sample code which logs background time remaining and its 180 seconds. I have made one test app from his sample code and verified it. – mitesh Feb 02 '15 at 18:09
4

Theorically, you have 2/3 minutes to close the tasks you want to do in background, if you don't do it, your app can be killed.

After that, you can call 'beginBackgroundTaskWithExpirationHandler 'and you have to be prepared just in case the 'little extra time' that Apple gives is not enough for the tasks you need to finish.

EDIT:

When an iOS application goes to the background, are lengthy tasks paused?:

From the documentation:

Return from applicationDidEnterBackground(_:) as quickly as possible. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method doesn’t return before time runs out, your app is terminated and purged from memory.

From Raywenderlich:

'Again, there are no guarantees and the API documentation doesn’t even give a ballpark number – so don’t rely on this number. You might get 5 minutes or 5 seconds, so your app needs to be prepared for anything!':

http://www.raywenderlich.com/29948/backgrounding-for-ios

How much time you get after your app gets backgrounded is determined by iOS. There are no guarantees on the time you’re granted, but you can always check the backgroundTimeRemaining property of UIApplication. This will tell you how much time you have left. The general, observation-based consensus is that usually, you get 10 minutes. Again, there are no guarantees and the API documentation doesn’t even give a ballpark number – so don’t rely on this number. You might get 5 minutes or 5 seconds, so your app needs to be prepared for anything!

Michal Šrůtek
  • 1,647
  • 16
  • 17
santibernaldo
  • 825
  • 1
  • 11
  • 26
  • 4
    this is incorrect, you usually get between 5 to 10 minutes from the system after asking for a background task – Kaan Dedeoglu Feb 02 '15 at 11:03
  • I think this comment is right. But I can't mark comment as correct right? – donkey Feb 02 '15 at 12:13
  • 1
    @KaanDedeoglu You haven't provided any sources for your opinion that you "usually get 5 to 10 minutes". I'm guessing things have changed and, at most, you only get 3 minutes. I haven't been able to get my background execution to run any longer than 3 minutes. I'd like to know when this changed. – Andrew Nov 20 '19 at 17:01
  • @Andrew, I haven't observed single background task running beyond 30 sec :(. The simulator also provides 30sec for background execution. I thought the result might be a variable number for a device... depending on current 'device stress' , frequency of app usage etc. – NightFuryLxD Feb 03 '23 at 08:22
1

It is not fixed: in Xcode 10 and Swift4.1

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    print(UIApplication.shared.backgroundTimeRemaining)


} 

OP1: 166.13057912699878 mean approx 2.7 min, OP2: 177.4997792619979 mean approx 2.95 min

Mantosh Kumar
  • 275
  • 1
  • 3
  • 8