Ninja Apps: Harnessing the power of BackgroundTasks in Swift
How your apps can operate from within the shadows
Power is important. The longer an app runs in the background, the more power it drains. If other apps are running in the background, it might even slow down an app you’re currently using. This is how it might look.
So how can this be improved? How can we make it better? With Apple’s new ways of implementing background tasks, we can control when apps are used in the background, and even use their ML Core to figure out the best time. So that our phones can instead look like this. Let’s go over each of them.
Background Task Completion
Background task completions can be used for sending messages, saving files, or handling requests from a database. It does this by completing tasks started in the foreground, running them in the background.
What this does is run the task of sending the message in the background, but if it takes too long, the operation ends, meaning it will not go on for an unneeded amount of time. If this occurs, the user gets a notification, telling them that it didn’t send. This prevents apps from taking up too much power and processing time. It also can set en earliest begin date, in this case 15 minutes after the user closes the app.
Background Pushes: Alert the App, not the User
Very useful for muted threads, can tell the app that content is available without having to alert the user. The app will receive the push, then download new data when able, all running in the background.
Downloading Old Data — When and How to Do it
If your user is downloading an App they used on a previous phone, they may not want to wait 15 minutes for all their old data to download. By using a Discretionary Background URL Session, such tasks can be deferred until another time, improving user experience. Can also set time intervals, earliest begin dates, etc.
BackgroundTasks: A Framework for Scheduling Background Work
Apple’s new Background Processing Tasks is the most exciting. This enables deferrable maintenance work, using core ML training to figure out the best time for it. For instance, does a user consistently use an app at 9AM? Or every Monday? With ML Training, the phone will know to update such apps before these times. This is done using Apple’s Background App Refresh Task. It can also use full CPU power, when the device is plugged in, for intensive work that needs to be done. This is something previously unavailable for normal applications.
BGTask Scheduler allows you to schedule your app to get items or run tasks in the background, just be prepared to concurrently handle tasks in the time the scheduler allots your app. Or else they won’t be completed.
How to
Go to your main project folder, go to Signing and Capabilities, click the ‘+’ sign to add a new capability, and search and add Background Modes. Checkmark background fetch.
Now go to Info.plist, and add ‘Permitted Background Task Schedule Identifiers’. Simply add a new string to it, giving it the name you want for the identifier. Next, Import background tasks in App Delegate.
Once you do this, you can create BGTaskSchedulers, registering the identifier of your task, and running any BGTask functions you create, such as a task to refresh your app after a certain amount of time.
Here we register our item, call a refresh helper function, and schedule it under a time interval of 15 * 68 (15 minutes later). Now we just need to define that helper function.
This function simply uses a BGAppRefreshTask to add a task to the queue. In this case, it’s just changing the value of the string we defined earlier, but it can be used for anything. What this will do for the UI is switch the label from what it is now (“Good Morning, Starshine, the Earth says Hello”) to what it’ll be 15 minutes after the app closes, provided it has enough time to complete the task (“You twinkle above us, we twinkle below”). This can be used, however, to handle data downloads, cache cleaning, or anything else needed for your app to stay pristine and up-to-date.
You must set tasks to be completed when done, or iOS might terminate your app running in the background in the future. It’s a good idea to set a ‘begin date’ for refreshing to be within a week, but not too often, or it might slow processes down. You can also define whether the task requires external power, or needs a wifi connection using requiresExternalPower or requiresNetworkActivity respectively. You can even test it, using Apple’s Debugger, by moving ‘forward’ in time.
That about closes it, you can watch Apple’s entire WWDC talk about it here: https://developer.apple.com/videos/play/wwdc2019/707/