How long does Apple permit a background task to run?
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
NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
will have to use @Gary Riches's suggestion
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.
How do you extend an iOS app's background execution time when continuing an upload operation?
You have stumbled across a less-than-stellar code sample in Apple’s documentation.
First, if you perform a synchronous network request, you definitely should dispatch it to a background queue. If you don't, you risk having the watchdog process kill your app. But you shouldn’t dispatch the network request synchronously to the global queue, but rather asynchronously, or else you just end up with the same problem, namely blocking the main thread.
That having been said, one really should never perform network requests synchronously. You should perform them asynchronously and end the background task in the completion handler.
In that example, they use
NSData
, which we don’t use anymore.Also
UIBackgroundTaskInvalid
doesn’t exist anymore. It is nowUIBackgroundTaskIdentifier.invalid
.In Apple’s defense, the point of this code sample is the background task, not the network code. They really didn’t want to get into the weeds of the implementation of this network code and were trying to keep it simple. That having been said, it really is a horrible and outdated code example.
See this answer for a better example of how one might use background tasks. Also, if the upload might take more than 30 seconds, we wouldn’t use the background task at all, but a proper (but more complicated) background URLSession
. (See Downloading files in the background. Upload tasks follow the same basic pattern outlined there, though make sure to upload from a file, not a Data
.)
Related Topics
Uisearchcontroller Persisting After Segue
Why Would a 'Scheduledtimer' Fire Properly When Setup Outside a Block, But Not Within a Block
Error Itms-90086 Submitting App
Nsnotificationcenter Swift 3.0 on Keyboard Show and Hide
Perform a Deeplink from Swiftui Widget on Tap
Exception Type: Exc_Crash (Sigabrt)
How to Use Http Live Streaming Protocol in iPhone Sdk 3.0
Deferredlocationupdatesavailable Returns No in iOS 10
Uisearchdisplaycontroller Without Dimming
Get Dns Server Ip from iPhone Settings
Region Monitoring Glitch on iOS 7 - Multiple Notifications at the Same Time
Search Bar and Search Display Controller in Table View
Adding Unlimited Lines in a Text (Swiftui)
Passing Data Back from a Modal View in Watchkit
iOS Autolayout and Uitoolbar/Uibarbuttonitems
How to Connect Multiple Buttons in a Storyboard to a Single Action
How to Parse Iso 8601 Using Nsdateformatter with Optional Milliseconds Part