This article originally appeared in TidBITS on 2010-06-23 at 11:42 a.m.
The permanent URL for this article is: http://tidbits.com/article/11378
Include images: Off

What is Fast App Switching?

by Matt Neuburg

Those of you who have just used iTunes [1] to install a shiny new iOS 4 [2] on your iPhone or iPod touch, or who are about to obtain a shiny new iPhone 4 [3], may find yourselves wondering about one of the touted features of this new system, fast app switching. What is fast app switching? What does it have to do with multitasking? Does it operate only through the new Home button double-press behavior? And why, for most apps, does nothing very remarkable or new seem to be happening?

Return with us now to those thrilling days of yesteryear (was it really just four months ago?), when Adam penned his provocative "Does the iPhone OS Need Multitasking? [4]" (8 February 2010). As Adam pointed out then, in his prescient and trenchant analysis, multitasking means different things to different people. You might mean (1) what happens on Mac OS X, where multiple applications really do run simultaneously and you return from application A to a window of application B to find it in the state you left it. Or you might mean (2) the mere ability to switch rapidly between recently used applications, which save state on quitting so that they behave as if they'd remained in the state you left them. What Apple has actually implemented is something a little more ambitious than option 2, but still considerably less resource-intensive (and potentially dangerous) than option 1.

Here's the long and short of it. When you are running an app in iOS 4 and you press the Home button once to leave it, the app doesn't quit. Instead, it goes into suspended animation, like the scientists in cryogenic hibernation in "2001: A Space Odyssey." The app simply stops receiving events from the system; its run loop isn't looping. The app is both backgrounded and inert; but it is still "running," in the sense that its resources and interface are still present, so that it doesn't have to be relaunched from scratch in order to resume. That way, when you come back to that app, no matter how, the app can simply pick up doing what it was doing when you left off, instantly.

This behavior is fast app switching, and is most of what Apple calls multitasking on iOS 4. (It is not the whole of iOS 4 multitasking, because some apps with specialized functionality will register with the system to be allowed to go into suspended animation with one thread still active; such functionality is strictly limited to playing audio, location detection, and voice-over-IP. But in this article I'm not concerned with those situations.)

The fast app switching interface that you get when you double-press the Home button (a gesture also correctly predicted by Adam's article) is thus all but irrelevant to the story. Indeed, it comes as a surprise to early adopters of iOS 4.0 to realize that the fast app switching interface does not list running apps. It lists recently used apps. It's a convenient way to leave an app and launch (or resume) a different, recently used app, but it could just as well have been present back in the earliest versions of the iPhone OS. It's mere interface, the iPhone equivalent of the Mac's Command-Tab switcher. It's also a very good, very welcome interface; I wish the iPhone had worked like this all along, because switching between apps, especially between a specific pair of apps, is something I do very often. But it has nothing to do with the underlying multitasking technology of fast app switching.

You actually have no easy way to learn what apps are running (suspended). When you launch or resume an app, you can usually tell the difference between whether the app was terminated or suspended, because a terminated app will go through its launch procedure (which may involve a splash screen and other obvious clues), whereas a suspended app will just appear, instantly, right where you left it. And you have no easy way to truly quit (terminate) an app. When you leave an app, no matter how - whether you use the fast app switching interface or just single-press the Home button - the system does what it does; it isn't up to you.

Therefore, you should be wondering at this point how an app ever really quits. Surely your iPhone won't be filled with the frozen bodies of dozens of suspended apps until you reboot? There are three ways an app can be genuinely terminated:

That final point is key, especially because it contradicts everything I've said about multitasking so far. It turns out that in order to participate in multitasking and allow itself to be suspended, every app must be recompiled for iOS 4. An app that doesn't appear to behave any differently when you resume it on iOS 4 from how it behaved when you relaunched it on earlier versions of the system simply hasn't been recompiled yet. That, as a moment's reflection will show, would be the vast majority of apps!

Clearly it will take some time for developers to recompile for iOS 4 and get their updates past Apple's App Store gatekeepers and onto your device. Until they do, you won't see all that much benefit from multitasking on iOS 4. Only Apple's own apps, and those few apps that have already been updated, are acting in a new way.

Moreover, recompiling for iOS 4 is non-trivial (as I just found out while doing it for the TidBITS News app - see "Free TidBITS News iPhone App [6]," 4 January 2010), because it will also require some rewriting. The app instantly participates in multitasking with no changes in code, merely by virtue of linking to the new iOS 4.0 frameworks - but that doesn't make it a good multitasking citizen.

One major issue is that an iOS 4-native app is notified when it is suspended, but not when it is terminated. Thus, it must do all the things to save state when it is suspended that it used to when it was terminated, just in case it later is terminated. Another issue is that the app, as it is suspended, needs to stop doing things that might cause trouble later. It must explicitly reduce its memory use if it doesn't want to be a candidate for later background termination by the system. It must cease any network activity. It may have to cancel a modal state, such as an alert that might not make sense when the user resumes later (possibly days later).

Those are all things I had to worry about when updating the TidBITS News app for iOS 4.0. Basically I had to consider every state the app might be in at the moment the user comes along and suspends it. That turned out to be remarkably difficult - and the TidBITS News app is very simple and small! Imagine, then, how long it will probably take before your favorite third-party apps are updated.

But when they are updated, you'll be switching between them with lightning speed. That's when you'll really experience fast app switching, leaving an app and coming back to it later to find it immediately ready to resume from where you left it. That's iOS 4's version of multitasking.

[1]: http://db.tidbits.com/article/11361
[2]: http://db.tidbits.com/article/11373
[3]: http://db.tidbits.com/article/11360
[4]: http://db.tidbits.com/article/10989
[5]: http://db.tidbits.com/resources/2010-06/Terminate-app.jpg
[6]: http://db.tidbits.com/article/10876