views:

85

answers:

4

I have put NSLogs in all my classes including my UIApplicationDelegate subclass. I am curious - and a bit nervous - about why I am not seeing them echo anything when I press the home button. I am running in the XCode simulator.

Since iPhone/iPad runs a single app at a time, doesn't hitting the home button discard all traces of the running app?

Thanks,
Doug

+2  A: 

When an app is terminated, its memory is simply freed. Dealloc is not called, it does not pass go or collect $200. This is normal and intended.

Chuck
Thanks Chuck. Before I give you the check mark on this question I have a closely related questions: Why on relaunching my app by clicking on it, do I not see my classes initialization methods fire (which are NSlog-ing as well)? It is as if alloc/init and dealloc play no part anymore after the initial launch of the app.
dugla
@dugla: Objects in nibs are archived, so they don't go through the normal initialization methods when they're unarchived. That's just a general property of objects in nibs. Some of the other objects that the framework creates at launch may be created with private initializers too — that I'm honestly not sure of, but it wouldn't surprised me at all. If you're talking about something other than what I mentioned here, you might try asking another question about app launch with more details.
Chuck
Chuck, I am not referring to the unarchiving of nib based objects. I am referring to the custom classes I've implemented - view controllers, views, model objects, etc. - that have their own alloc/init and dealloc methods. Nothing clever going on here just garden variety stuff. My question is why am I seeing very different behavior on initial launch then on re-launch. Again, this is all withing the Xcode simulator.
dugla
Got it. It turns out on app relaunch, NSlog-ing is not echoed in the Simulator. Whatever. Cool.
dugla
+3  A: 

Chuck is correct, the dealloc's don't matter at that point. If you want to do something as your app is expiring, implement this in your app delegate class:

- (void)applicationWillTerminate:(UIApplication *)application {
   // goodbye...
}
wkw
A: 

You should implement -(void)applicationWillTerminate:(UIApplication*)application{ } this method will get called when user hit home button.

iamiPhone
A: 

You might also want to think about the additional multitasking support that has been announced. Obviously the details are still under NDA, but pushing Home does not necessarily stop your app.

And think about the various circumstances where your app may already be terminated quickly (e.g. if a call comes in and the system runs out of RAM). Do your cleanup with the application lifecycle messages.

David Dunham