Background tasks and why it is interesting?

What is iOS Background?

The iOS operating system manages different states for the applications which have been installed on the devices.

iOS background

As we can see on the image, there are five states and two main phases: Background and Foreground, which contain various status.

When a user using an application and he taps the Home button, the current application is setting on Background mode. From this moment on, the application has a maximum time to finalize the operations in course. This time depends on the operating system.

How do the applications on background mode work?

On iOS 6, when an application is setting on background mode, it can be used to do operations in background during a maximum period of 10 minutes. Once this time has expired, the application is automatically suspended by the system, and it’s not possible to execute any operations after. On iOS7 the maximum time was reduced to 3 minutes.

Some of the applications available on the market such as those that use the music player, the ones that use the microphone or those that need the user location, are detected by the system and they won’t’ be suspended whenever they get into background mode. However, as a developer user, it could be interesting to implement other types of operations, such as the file transfers, even if the time has expired.

NSURLSession

With this target, iOS7 includes a new class: NSURLSession, which allows the use of sessions on the code. The target of this class is to improve the current one: NSURLConnections. An object of this class has tasks: NSURLSessionTask, in this way it is possible to manage the main HTTP operations in background mode.

There are three different tasks:
– Tasks to manage the data: NSURLSessionDataTask
– Tasks to upload data: NSURLSessionUploadTask
– Tasks to download data: NSURLSessionDownloadTask

With this new class it is possible to create a queue with tasks to continue the natural flow even if the application had been suspended.

This important improvement allows to the iOS applications to execute operations in background mode. However, it’s recommended to control the background tasks in order to avoid problem with the battery consumption.

Are you able to improve your application using NSURLSession? Here you can obtain more detailed information about NSURLSession and some examples:

NSURLSession Class Reference
Networking with NSURLSession

In next post, we are going to talk about the use of the background mode in some of our applications.

Leave a Comment

¿Necesitas una estimación?

Calcula ahora