0

I have a simple question about one of my iPhone applications.

I have a few global counters that store numbers from 0 up to 13, and once the program is terminated (click stop on simulator) and reopened in debug mode, the counters still have the same information as the previous run. Where in my code should I reset all my counters? Ideally I only want them to be reset if you terminate the program from the iPhone multitasking feature (double tap home button and exit program).

There isn't any code that is necessary to see, and I don't want to have a button that you can press to reset the counters either.

Any suggestions?

Till
  • 27,559
  • 13
  • 88
  • 122
James Brown
  • 919
  • 3
  • 13
  • 22
  • How are these counters defined/implemented? – Firoze Lafeer Mar 16 '13 at 02:20
  • just globally in the viewController.m file... int someCounter = 0... then modified when my UI logic changes it – James Brown Mar 16 '13 at 02:22
  • 2
    globals don't just automatically persist when you terminate your app. They will be initialized each time your app starts. If your app is not behaving that way, then you do need to show some code. – Firoze Lafeer Mar 16 '13 at 02:27
  • @FirozeLafeer is entirely right. You need to make sure you understand the difference between fast app switching and restarting an app. The former keeps your app status intact, the latter does not and you will have to manually persist any information that needs to survive that. – Till Mar 16 '13 at 02:43

1 Answers1

1

Assume the counters are persisted (stored somewhere when your app exits). There are a couple of ways you can go about it:

You can set the counters in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

or reset them to 0 in

- (void)applicationWillTerminate:(UIApplication *)application

Those two functions are found in AppDelegate.m