How long does Apple permit a background task to run?

ios background task example swift
background tasks ios 13
ios background task example objective-c
how to run background service in ios swift
can t end backgroundtask: no background task exists with identifier
iphone background tasks
ios run app in background
call api in background ios swift

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?

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.

Extending Your App's Background Execution Time, The system runs only tasks registered with identifiers on a list of permitted task Add a new item to the list and choose “Permitted background task scheduler� Call the method before performing any long-running task. Listing 1 shows an example that configures a background task so that the app may save data to its server, which could take longer than five seconds. The begin Background Task(with Name: expiration Handler:) method returns an identifier that you must save and pass to the end Background

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]);

BGTaskScheduler, Mark the start of a task that should continue if the app enters the background. This method returns UIBackgroundTaskInvalid if running in the background is not possible. Marks the end of a specific long-running background task. Developer Forums � Bug Reporting � License Agreements � System Status � Contact Us� Goldman Sachs 1 uses your credit score, your credit report, and the income you report on your application when reviewing your Apple Card application. This article highlights a number of factors that Goldman Sachs uses, in combination, to make credit decisions but doesn't include all of the details, factors, scores or other information used to make those decisions.

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.

EDIT Here is a nice article that describes background task run time and how to achieve long running background task in iOS. https://gooddevbaddev.wordpress.com/2013/10/22/ios-7-running-location-based-apps-in-the-background/

beginBackgroundTaskWithName:expirationHandler, Use the BackgroundTasks framework to keep your app content up to date and run tasks requiring minutes to complete while your app is in the background. The following are the only scenarios in which a third-party iOS app can use CPU in the background. Background Fetch. Any app can use "Background Fetch" (aka Background app refresh) to fetch content in the background for a few seconds, about once a day (on a timeframe regulated by iOS, out of the developer's control).

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

BackgroundTasks, When our iOS app authenticates with the server, a background thread is How can I allow for more time in the background before the expirationHandler is called ? tasks, the only way to know how much time you'll get is to actually use it. 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.

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 iOS App Programming Guide:

Your app delegate’s applicationDidEnterBackground: method has approximately 5 seconds to finish any tasks and return. In practice, this method should return as quickly as possible. If the method does not return before time runs out, your app is killed and purged from memory. If you still need more time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request background execution time and then start any long-running tasks in a secondary thread. Regardless of whether you start any background tasks, the applicationDidEnterBackground: method must still exit within 5 seconds.

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!

BackgroundTaskWithExpirationHandler: thread , The system may also launch an app directly into the background state, or move a suspended app into the background, and give it time to perform important tasks. Not that long ago, iOS apps running in the background on your iPhone or iPad were essentially stuck in suspended animation, unable to do anything (besides trigger alerts via Apple’s “push

Preparing Your UI to Run in the Background, It must run quickly, that is, in less than a second or so. If it takes too long, your app will be killed by the watchdog. Your expiry handler is called on the main thread. Of the two, I prefer Task (it's much easier to clean up progress reporting than to permit nesting). They're both light years ahead of other common solutions, though. I cringe when I hear of people using Thread or ThreadPool.QueueUserWorkItem. They are the absolute hardest to use correctly for background tasks. – Stephen Cleary Aug 26 '10 at 3:25

UIApplication Background Task Notes, I've read many posts about running tasks in the background, however I have not been able to get an answer to our Your timer is long overdue, so it'll fire now. What Does a Background Check Show? The contents of a background check will vary depending on the industry, the type of job an applicant is seeking, as well as the employer’s preference. The most common background checks consist of criminal history, education, previous employment verifications, and reference checks.

How to run timer in background for…, The maximum amount of time remaining for the app to run in the background. Availability. iOS 4.0+ Marks the end of a specific long-running background task. I want to use Watch Connectivity and Accelerometer in background task by watchOS 2 app. However, the app will die soon as a transition to the background. So I can not run any tasks in background mode. For example, I make a app for golf swing sensor, it is troubled because Apple Watch sleep as soon as twisting the wrist.

Comments
  • If you are transferring data then the best approach would be to use NSURLSession as described in the iOS programming guide
  • I am not downloading data, but uploading some images using parse. I am not sure NSURLSession is the most efficient approach
  • Im getting background task remaning time as 0. but still mu method is calling?
  • 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.
  • 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
  • @oasisweng check this link.
  • Thanks, I have stumbled a good article I find it worth sharing. link 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.
  • 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.
  • this is incorrect, you usually get between 5 to 10 minutes from the system after asking for a background task
  • I think this comment is right. But I can't mark comment as correct right?
  • @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.