Background Fetch Implementation

RapidValue Solutions
5 min readOct 5, 2015

--

Among millions of existing applications, most of them fetch data from the web. With iOS 7, Apple has introduced a feature to allow applications to run in the background — this is called ‘Background Fetch’. After enabling this feature, it allows application to fetch the latest data from the web with a goal to refresh its content without draining system resources or the user’s battery. For example, if you are implementing news feed in your application, when the application is launched from the background, it takes sometime to load the latest data and the user is looking at the old data for at least several seconds. It would have been better, if the new data was available at the time when the application was opened from the background. This is exactly what Background Fetch provides.

An important feature of the Background Fetch is its ability to learn the number of times that should allow an application to fetch the latest data and get updated. For example, if a user opens the application every morning at about 9:00 a.m., after some usage, the system predicts that next time the application will get launched at the same time, hence, it ensures that background fetch happens and gets updated before the usual launch time. That way, when the user launches the application, the new and refreshed content is available. This feature is called Usage Prediction.

Background Fetch Frequency

How frequent our application can perform Background Fetch is determined by the system and it depends on:

  • Whether network connectivity is available at that particular time
  • Whether the device is awake i.e. running
  • How much time and data your application has consumed in the previous Background Fetch

Background Fetch Mode Set-up

Steps needed to set up Background Fetch mode:

  • Go to your project settings and then access “Capabilities” pane
  • Turn on “Background Modes” , if it is not already done
  • Check “Background fetch” check box

Turning on the Background fetch capability for your application

Implementing Background Fetch

In order to implement Background Fetch there are two things you must do:

Use setMinimumBackgroundFetchInterval(_:)to set appropriate time interval for Background
Implement application (:performFetchWithCompletionHandler:) delegate in your application to handle the Background
In the application:didFinishLaunchingWithOptions: method of AppDelegate, in order to indicate that your application wishes to perform Background Fetch , use setMinimumBackgroundFetchInterval: method of the UIApplication object. This method takes a value indicating the minimum number of seconds to wait between Background Fetches.

We can use UIApplicationBackgroundFetchIntervalMinimum constant, which is the shortest fetch interval supported by the system. If you want to stop Background Fetch, you can use the UIApplicationBackgroundFetchIntervalNever constant.

When the system wakes and runs the application on the background, in order to use the Background Fetch, the application:performFetchWithCompletionHandler: delegate method is being called. We must implement all the logic, required for our application, to properly fetch data, manage it and update the UI in this method. When downloading any content, it is recommended that you use the NSURLSession class to initiate and manage your downloads.

There are three arguments that can be provided to indicate fetch completion in completionHandler:

  • UIBackgroundFetchResultNoData: Indicates that no new data was found.
  • UIBackgroundFetchResultNewData: Indicates that new data was found.
  • UIBackgroundFetchResultFailed: Indicates that it failed to fetch new data.

Testing Background Fetch

One way to test background fetch is on an actual device and waiting for the system to decide to do it. That would require a lot of time. Fortunately, Xcode provides a way to simulate a Background Fetch. First, deploy the application onto the iPhone Simulator. Select the Debug > Simulate Background Fetch menu item in Xcode (see Figure below). Your application on the iPhone Simulator will now go into the background.

Once you have used Xcode to simulate a Background Fetch for your application, look at the Output window .You should see the printout as shown in Figure 3. The two statements printed confirm that the Background Fetch method has been called.

The printout confirms that the Background Fetch method has been called

Advantages

  • Fetch latest data from the web with a goal to refresh its content
  • Can set badge on application icon to represent notification, without using remote notification
  • Avoid waiting time for refreshing data
  • Active, frequently, without draining much battery

Disadvantages

  • The execution time, for the application, is not finite while in background
  • System provides only 30 seconds time frame in order to fetch new data, update its interface and then go back to sleep again
  • Since system decides the best time to issue Background Fetch, this cannot be used for critical updates.

Conclusion

The Background Fetch API is one of the most popular and useful feature that has been introduced in iOS. It adds effectiveness and usefulness to the way your applications perform. The application can fetch the latest data from web with the intent of refreshing its content, without draining the user’s battery. The content is up-to-date when the user launches the application from background. Using this feature, you can now create applications that will totally change the experience of users. Customer satisfaction is of utmost importance and creating happy users is a priority. This is kept in mind while building an application and the Background Fetch is one of the factors that help you to achieve your desired goal.

By,

Syamily M.T.

iOS Developer, RapidValue Solutions

www.rapidvaluesolutions.com

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

RapidValue Solutions
RapidValue Solutions

Written by RapidValue Solutions

RapidValue is a leading provider of end-to-end mobility, Omni-channel, IoT, AI, RPA and cloud solutions to enterprises worldwide.

No responses yet

Write a response