iOS 7: Background Fetch

Written by: on September 18, 2013

Some applications require periodic updates of data from a remote server.

For example, a sports app might fetch the latest game scores. The new Background Fetch capability allows your application to ask to fetch data on a regular basis, so when the user launches or re-opens your application, it can start up with the most current data possible.

Background fetches are synchronized between applications to minimize power impact; in addition, the quicker your application’s fetches are, the more often your app will have opportunity to perform fetches (conversely, the longer your app takes and the more power it uses, it will have increasingly fewer opportunities to fetch data).

In order for your app to participate in background fetch, you have to first enable the Background fetch capability. To turn on background fetch, find the “Capabilities” tab of the project settings screen. Turn on the “Background Modes” capability and select the “Background Fetch” mode. This adds the requisite entry to the info plist file for your application.

Background-Fetch-Updates

Next, in your application’s application:didFinishLaunchingWithOptions: delegate method, set the minimum fetch interval. Typically, you’ll set this to UIApplicationBackgroundFetchIntervalMinimum to fetch as often as allowed:

The default value is UIApplicationBackgroundFetchIntervalNever, so if you fail to set the value on launching, your app will never be woken up to perform background fetches. The value you provide is the minimum number of seconds between wakes; do note, however, that the value is merely advisory—your application may perform fetches less or more often.

When your app is given a chance to perform a background fetch, its application:performFetchWithCompletionHandler: delegate method is called. This method gives your app opportunity to instantiate and start a fetch using an NSURLSession with the download session type. You must call the completion handler provided within 30 seconds, or your app will get substantially fewer background fetch opportunities later.

The completion handler takes three results: UIBackgroundFetchResultNewData, if new data was found and processed, UIBackgroundFetchResultNoData if no new data was found, and UIBackgroundFetchResultFailed if an error occurred during the fetch.

You can combine the use of NSURLSession fetches to fetch new data and background fetch to process the data, particularly if your fetch requests can often take longer than 30 seconds. For example, suppose you have a BackgroundFetchManager class for marshaling background fetches:

Xcode 5 provides two methods for testing background fetch. The first is to debug the app as you normally do through Xcode, and while it is running, select Debug > Simulate Background Fetch. The second is to create a new scheme that simulates launching due to a background fetch event. To create a scheme for testing background fetch, duplicate an existing scheme, then select ‘Launch due to background fetch event’ in the Scheme’s Options under the Run settings.

This is the seventh part in an 11-part Developer’s Guide to iOS 7. You can find the full guide here. For more information on how Double Encore can help you prepare your company for the changes in iOS 7, please email us.

Kevin Tatroe

Kevin Tatroe

Kevin Tatroe is a Software Engineer at Double Encore, specializing in iOS Development. Kevin is a published author and very active in the Cocoa community.
Article


Add your voice to the discussion: