Document directory
- The world before multitasking
- Multitasking on
- Conclusion
Address: http://www.drobnik.com/touch/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
Update: added handleopenurl to the flow charts. Added uiapplicationexitsonsuspend. untangled some lines.
Now that we all are moving our source code gradually to iOS 4 I had to pause and think a bit about where to move which code. A problem that I'm facing frequently when updating a project is that the didfinishlaunching is only called if the app really launches.
That poses a bit of a challenge if you are used to doing things like refreshing images or other files off the Internet. an app that is resumed from standby does no longer go through this delegate method. so an app that wowould always show fresh content upon launch before 4.0 multitasking wowould no longer load any new content as soon as you build it for 4.0. that's actually one of the main reasons why I have not yet had time to update mydomainales to 4.0.
To gain the possibility for "Fast app switching" you actually don't need to do anything. all apps automatically support it because they no longer get terminated if the user pushes the Home button. they get put into a sleep mode while the iPhone still has enough memory for everything else. it's only if RAM runs out That the OS starts killing apps. there is begins with the ones that have the most memory reserved.
I grabbed the free trial of charge and the non-techie Process Flowchart stencils by gfraser. Then I researched when all these various delegate methods of uiapplication are being called and drew charts to parse strate the flow.
By inhaling first how it was before multitasking and then upgrading your mental process to Backgrounding we can begin to fully appreciate how it all fits together.
The world before multitasking
The first thing that I learned from this is that applicationdidbecomeactive: is being called right after application: didfinishlaunchingwitexceptions :. so even before 4.0 It wowould have been smart to put there any kind of code that you want executed whenever the app becomes active, be it after app start or upon rejecting a phone call.
Not shown on this divisor is the situation that your app is eating up all the system memory and ignores both the level 1 and level 2 memory warnings. in that case the OS will also terminate your app without totally unscrupulous.
The Mechanism with willresignactive and didbecomeactive is sort of the Backgrounding Lite that has always been there. this occurs whenever some other thing is displayed in front of your app. this cocould be the UI to accept a phone call or something as benign as the synching screen.
Multitasking on
Adding multitasking to this flow chart makes it about twice as complex. I probably spent an hour trying to fit it all together with no crossing lines. another thing that I noticed while doing this is that applicationwillresignactive: is the only delegate method that I cocould not optimize into a single box, because one use is to show that the app has been interrupted, the other is to the path into the background.
An app built with 3.2 will still go through a short background stage where applicationdidenterbackground: is called. but then it will be terminated as usual. if the same app is built against a 4.x SDK then it will not go straight from background to being terminated. instead it will stick around and so will be the debugger. as soon as you tap the app icon again or choose it from the app switcher it is first informed that applicationwillenterforeground and then applicationdidbecomeactive.
An app that is suincluded but still in Ram might be terminated by the OS if memory runs low. but after entering the background there is no longer any delegate method being called, instead the app will receive a kill signal. this occurs also if the user long-presses the app icon in app switcher and then removes the app from there with the removal button.
Accepting a phone call no longer terminates your app but will send it into the appropriate background mode. within the background box you see two purple states that the app can be in. all apps are suincluded in Ram per default. you might want to slim down your memory footprint to make your app a less likely target for killing if the system runs low on memory.
Only apps that actually require background processing continue to be executing. this requirement is either set in info. plist or by telling the OS that you have a task to complete, like for example a file upload.
The following kinds of activities get to run in true multi tasking behind the foreground app with lesser CPU priority.
- Playing music: "APP plays audio"
- Geo location: "app registers for location updates"
- VoIP: "app provides voice over IP services"
If your app does one of these things then adding the key to info. plist is all it takes. but be aware that as soon as your app stops doing what it stated it wocould be doing the OS again will send it to sleep, only to be awakend by another gentle tap by the user.
In background mode you're advised to stop updating the UI. Especially with OpenGL apple is really strict. If you app as much as touches OpenGL while in background the app gets killed right away.
There are only two scenarios left where your app's applicationwillterminate: will actually be called: if it's building with an SDK of less than version 4.0 or if you chose to opt out of Backgrounding by adding the uiapplicationexitsonsuspend key to your info. plist.
So, let's ask the question we 've ve been doing all this fuss for: Where shocould you now put your code to initialize, update or save changes? Applicationwillterminate not being called in the majority of cases makes it a rather bad candidate to save user ults or data.
Looking at the chart I see applicationdidenterbackground as the ideal candidate delegate Method for saving state. this is also the last method to be called before a regular 4.0 app gets put to sleep and before a possible kill.
For updating data from the web the case is not as clear. you wocould not want to reload every time the user comes back to your app. possible applicationdidbecomeactive: is a good place, provided that you only reload data if sufficient time has passed to be able to assume that something new is available.
Only the first loading of data can stay in application: didfinishlaunchingwitexceptions: Since every call applicationdidbecomeactive: most likely has the data already loaded previusly.
Conclusion
Adding fast app switching and multitasking support is quite easy since for the most part you don't need to do anything. but you will have to review the contents of your app delegate methods to see which code has to be moved into one of the other delegate methods now. clean out applicationwillterminate: and have a close look at application: didfinishlaunchingwithoptions: To check if you have something in there that needs to be done every time the app becomes active.
These charts come from some experimentation that I did and what I remembered from the WWDC 2010 session videos on multitasking. potentially there are mistakes or more surprises hidden in there. please let me know if you find any.
Here's the chart in pdf format in case you want to print it out and frame it.