After your application completes its actions during a background fetch you must call the completionHandler block with one of the three UIBackgroundFetchResult
states: UIBackgroundFetchResultNoData
, UIBackgroundFetchResultNewData
, or UIBackgroundFetchResultFailed
.
How are each of these three results handled by the OS once the completion handler is called?
The Background Fetch API provides a method for managing downloads that may take a significant amount of time such as movies, audio files, and software.
The Background Fetching will NOT happen in your app after the user has killed it in the multitasking UI. This is by design.
From the iOS App App Programming guide:
When the application:performFetchWithCompletionHandler: method of your delegate is called, use that method to check for new content and to download that content if it is available. When your downloads are complete, execute the provided completion handler block, passing a result that indicates whether content was available. Executing this block tells the system that it can move your app back to the suspended state and evaluate its power usage. Apps that download small amounts of content quickly and accurately reflect when they had content to download are more likely to receive execution time in the future than apps that take longer to download their content
They don't give us so many details, but I think is clear enough: you pass the result of the fetch to the System, so it can decide when to give background execution time (and how much).
Example, consider two different apps:
- one downloads files that are updated every night
- the other downloads files that are updated more frequently, many times in a day
In both cases, the system will wake up your app, takes note of the start time, your app starts the download and then tells the system that there was or not content available.
After some time, you'll see that the system will wake up the first app less frequently than the second one, optimizing battery consumption.
Moreover, if you use NSURLSession to start you download, the system will evaluate your app's power consumption (since using NSURLSession you have "unlimited" time to download files), even this metric is used to decide how often wake up your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With