Implementing the prerequisites for background fetch

In order to implement background fetch, you will need to take the following three steps:

  1. Add the background fetch capability to your app.
  2. Ask iOS to wake your app up.
  3. Implement application(_:performFetchWithCompletionHandler:) in AppDelegate.

We'll implement step 1 and 2 right now; step 3 will be implemented separately because this step will involve writing the code to fetch and update the movies using a helper struct.

Adding the background fetch capabilities

Every application has a list of capabilities they can opt in for. Some examples of the capabilities are Maps, Home Kit, and Background Modes. You can find the capabilities tab in your project settings. If you select your project in the file navigator, you can see the Capabilities tab right next to your app's General settings.

If you select this tab, you can see a list of all the capabilities your app can implement. If you expand one of these capabilities, you're informed about what the capability does and what happens automatically if you enable it. If you expand the Background Modes capability, you'll see the following information:

Adding the background fetch capabilities

There are several modes available to our app, but we're only interested in one at this time. We want to implement Background fetch. Click on the on/off checkbox to enable this capability for our application and check the Background fetch option.

Enabling Background fetch isn't something that magically changes some invisible settings for your application. You could actually enable background fetch manually if you'd like to do so.

Open up the Info.plist file and search for the Required background modes key. You can see that it contains a single item with a value of App downloads content from the network. This entry in the Info.plist enables your app to request iOS to wake it up periodically:

Adding the background fetch capabilities

However, just because we can do this manually does not mean we should. The capabilities tab is a very convenient way for us to manage capabilities, and manually adding the right key to the Info.plist is tedious and error prone. It's interesting to know that there's no magic involved in this process, and it makes the entire feature feel more transparent.

Asking iOS to wake our app up

We're halfway there in terms of enabling background fetching for our app. All that's left to do is inform iOS about our desire to be woken up every once in a while. We will implement this behavior in AppDelegate's application(_:didFinishLaunchingWithOptions:). We will need to implement our request in the preceding method because we would have to ask iOS to wake us every time the app launches. Once the app is launched, it can make the transition from the foreground to the background, which is when our app should be woken up. However, if the app is killed entirely, iOS won't be able to wake our app up again, so we will need to ask it to do so the next time the app launches.

There isn't much code involved to enable background fetching for your app. Add the following line to application(_:didFinishLaunchingWithOptions:):

application.setMinimumBackgroundFetchInterval( 
UIApplicationBackgroundFetchIntervalMinimum) 

This line will ask iOS to wake us up at a minimum interval. This means that we're woken up as often as iOS will allow us to be woken up. It's impossible to predict how often this will be since iOS will throttle this interval as it sees fit. If your app often has new data available, or is used many times throughout the day, odds are that your app will be woken up more often than it would be if you rarely have new data available or if the user opens your app once every couple of days.

Alternatively, you could set the fetch interval to UIApplicationBackgroundFetchIntervalNever if you want to prevent your app from being woken up or use a custom value. If you provide a custom value, iOS will attempt to honor your custom interval, but again, there are no guarantees with background fetch, as the system remains in charge in respect to the intervals that are actually used.

The only thing iOS does guarantee is that your app is not woken up more often than what you specified. So, let's say, if you specify a custom interval for your app to be awoken once every 3 hours, iOS won't wake your app up more often than that.

Our app is now ready for fetching data in the background. Let's go ahead and implement the feature next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.1.232