1

My game made using cocos2d crashes on iOS5 after resuming from the background when left for a while. I want to know what the standard/best practice is, on handling an app that is sent to the background. Do I terminate it after a certain time? I see some games pull up a loading screen when you resume it after a long time but when you resume it immediately it goes straight to the game. What are they loading when they resume?

Any pointers in the right direction would be much appreciated.

Thanks AC

AbhinavVinay
  • 303
  • 6
  • 18

1 Answers1

1

You can opt out of background execution by adding UIApplicationExitsOnSuspend to your Info.plist.

Other than that it really is your job to ensure that your app doesn't crash when it resumes execution. You have to understand that your application basically enters a suspended state. That means it should unload all unneeded resources, otherwise the system may terminate your app's process.

In your app delegate you should respond to the applicationDidBecomeActive message and respond accordingly so that your app is able to resume execution without any issues. This can include loading any unloaded assets and checking if system settings (ie. locale, Game Center user, etc.) have changed.

You can also register a didBecomeActive UINotification so that any class in your app gets notified when the app should resume.

Community
  • 1
  • 1
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks. I understand now. Would the following help to be called at applicationWillResignActive? [[CCTextureCache sharedTextureCache] removeUnusedTextures]; [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; – AbhinavVinay Oct 25 '11 at 19:00
  • yes but theres a director "purge" message that purges all caches – CodeSmile Oct 25 '11 at 20:11
  • But would I want to purge everything?? Then that would require me to reload everything again when I resume right? Is this safe? – AbhinavVinay Oct 25 '11 at 20:57
  • Cocos2D does this "reloading" automatically. But you do want to release as much memory because you can't hold on to megabytes of memory while your app is in the background. The iOS may terminate your app if another app running in the foreground needs more memory, and your app still uses a lot of memory that it doesn't release. – CodeSmile Oct 25 '11 at 22:28