articles

Home / DeveloperSection / Articles / How You Can Manage Background Tasks with the New Task Scheduler in iOS 13.

How You Can Manage Background Tasks with the New Task Scheduler in iOS 13.

How You Can Manage Background Tasks with the New Task Scheduler in iOS 13.

Joe Darzi4754 22-Jul-2019

In WWDC developer conference 2019, Apple has introduced iOS 13 with the extensive features and functionalities. It is the crucial and most important revision of operating system which is being used on more than 1 billion iOS devices across the globe. The most important thing is this latest version is more faster and efficient compare to the older version iOS 12. Along with this the app download size is also decreased to half 50%.

With these latest features of iOS 13, this is the time for using BackgroundTasks framework. Yes! We can use this framework to take leverage of scheduling and controlling the execution condition of our background jobs. You can make plan and schedule your BackgroundTasks as like battery charging, connection availability and many others. Then, this BackgroundTasks Framework will run the background work according to the required criteria.

The BackgroundTasks Framework has Various Types of Tasks Which Are as Follows:

1) App Refresh Task:

BGAppRefreshTask is one of the special background tasks which can be used for updating data of a mobile app. The user behavior is only thing which can make this task exclusive. The iOS determines how often and what time a user is accessing a particular app from their devices. After an interval of time, it will try to run BGAppRefreshTask when the user is not completely using that app.

Here are the few steps, which are required for implementing a background app refresh for a sample app. First of all, you have to include background modes capability in the settings of project. Secondly, you need to enable checkbox of “Background fetch” which is must required for background app refresh. Next, you need to add “Permitted background task scheduler identifiers” key to info.plist which will store the array of unique identifiers for each background task in the app. After that, you can start working on background refresh logic.

How You Can Manage Background Tasks with the New Task Scheduler in iOS 13.You can start by registering the task identifier and associated closure which will run on every execution of job. You will use handleAppRefresh function for running a background process. Here are the few primary points which you need to consider.
 

  • When your job is done then make sure you need to call the setTaskCompleted method.
  • Don’t forget to set expiration handler on a task object because the system will provide you a limited time to complete the entire job, and if you will exceed it then you have to clean up all the resources.
  • You need to keep this in mind that you can only schedule a job once, if you want to schedule more than one job then you have to schedule it every time which will run periodically.

How You Can Manage Background Tasks with the New Task Scheduler in iOS 13.

2) Background Processing Task:

This is another type of background tasks which is processing task. It can be used to train ML model on-device and also help to clean up in database. Before you start, you must enable checkbox of “Background processing” in Background Modes Capability. This code will help you to add another identifier for a new job type into info.plist.

 

How You Can Manage Background Tasks with the New Task Scheduler in iOS 13.A scheduling a processing a task is a same as the app refresh task. You can explore the scheduleMLtrain function and set a few important properties such as requiresExternalpower and requieresNetworkConnectivity into true which will help you to indicate your jobs requires a network connection and battery charging. I will definitely encourage you to do heavy calculations when your device is on charging mode to prevent battery drain and user experience.

3) Debugging:

For debugging background tasks you need to keep your phone connected to Xcode debugger, but you can’t estimate a time because only iOS will decide to run your entire job. Because it uses a few hidden logic for that. Fortunately, Apple can support two private functions which you can use in the debugger for starting and background task. Please make sure that you can’t add these functions at releasing time, you can only use them during the development time.

You can run below code in your debugger for pausing and starting backgrounds tasks in the app.

e -l objc — (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@”TASK_IDENTIFIER”]

To force early completion use:

e -l objc — (void)[[BGTaskScheduler sharedScheduler] _simulateExpirationForTaskWithIdentifier:@”TASK_IDENTIFIER”]

Make sure that you replace TASK_IDENTIFIER with the real identifier.

Wrap Up:

The Background Tasks framework is very efficient to use for scheduling your heavy work with better user experience by using environment conditions. At present WWDC 2019, Apple has launched iOS 13 with amazing features. Hence, if you are looking to build any iOS Mobile Application with the latest methodology you can concern your ideas with any Mobile App Development Company. Thanks for reading.


 


Leave Comment

Comments

Liked By