Basics of implementing multitasking on the iPhone

23 06 2010

One of the most highly anticipated features of Apple’s iOS 4 is the ability to temporarily push an application’s processes to the background while another application is brought to the foreground and used. This was covered in detail at WWDC earlier this month. Fortunately, Apple was generous enough to provide videos from the conference on its iPhone developer site.

Main types of multitasking app

There are three types of multitasking app, detailed in the UIBackgroundModes key.

  • Audio (This would be for an application like Pandora and would allow it continue playing music outside of the application, similar to the iPod app.)
  • Location (This would monitor the phone’s position via CoreLocation and perform tasks according to the developer’s code.)
  • VoIP (This would include applications like Skype and allow them to function like Apple’s “Phone” app.)

Application lifecycle

There are three states in a multitasking app’s lifecycle, Active, Inactive, and Background.

typedef enum {
} UIApplicationState;

When the application launches, it calls the following methods from the app delegate:

-(void) application:(UIApplication *)application    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
-(void) applicationDidBecomeActive:(UIApplication *)application;

When the app goes to the background, we will execute the following the app delegate:

-(void) applicationWillResignActive:(UIApplication *)application;
-(void) applicationDidEnterBackground:(UIApplication *)application;

When an app is pushed to the background, it operations are suspended, save for the the functions that are desired to run in the background. For example, you would want Pandora to keep playing music and occasionally retrieve more music. You would want Skype to continue taking input from the microphone, sending output to the speaker, and remaining in contact with Skype’s servers. It is primarily the GUI-related functionality that is suspended when an app is pushed to the background.

Killing apps

Now, the iOS is not merciless when it comes to killing app processes. An app is given some time to clean up any processes that are not intended to run in the background. There, its state is sitting in volatile memory.

Because paging memory to the disk is not allowed, if system resources are needed for an active application, any background application can be killed by the OS. When it is killed, it is again given some time to clean up. For example, an app might need to finish a download or safely disconnect from a server. This can be done with the following methods:

-(void) endBackgroundTask:(UIBackgroundTaskIdentifier)identifier;

The first line of that code will tell the application delegate to move the app to background status along with some instructions on how to handle being terminated while there. The method endBackgroundTask: essentially kills the background task it is given as an argument.

Closing remarks

So, that’s about it. Apple seems to have come up with a system for background processes that is pretty straightforward. The OS will decide for you if and when a background app is to be terminated. It takes only a few lines of code to get started with this stuff. Apple has posted the video of this talk, along with the slides. (It is in the video “Session 101 – What’s New in Cocoa Touch.”) All of the code that you see here came from that talk. I hope all this was helpful.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: