0

I've come across a strange error while programming my iPhone application. Basically when I leave my application in the background and then access it after a long time (i.e. the entire night while I'm sleeping), the viewDidLoad method seems to be called again even though I did not exit the app (I only double tapped the Home button or I tapped the Home button once) but still left the app in the background. However, if I leave the app on for a short period of time (anytime between a few minutes to a few hours), the viewDidLoad method is not called again and everything is as it should be. After doing some research, I found that it is because the viewDidUnload method is called (after the OS finds that the app is suspended for a long time), which calls viewDidLoad again when we bring the app back up. I found this out through this link: view seems to reload itself but it doesn't seem that there's a way to prevent viewDidLoad from being called when the viewDidUnload is called. Is there any way to prevent this viewDidUnload method from being called again? The thing is I want my app to be running for a long time in the background (i.e. a few days in the background) to collect data. Or, is there no way around this? Any help would be appreciated. Thanks!

EDIT: I have realized that after iOS 5, viewDidUnload is deprecated but this phenomenon still occurs. Any ideas on how to fix it? Thanks!

Community
  • 1
  • 1
user1871869
  • 3,317
  • 13
  • 56
  • 106
  • 2
    Apps are killed while in the background when needed. There is nothing you can do to stop that. – rmaddy Jun 01 '15 at 21:30
  • @rmaddy So is there any way I can cache that data even though the OS kills the app? It's just that I would want the app to be running for a long time and if the settings are "reset" like described above, it would throw off the user when using the app. This is an example of what I'm running into: I have a start and a stop button. Once I press it, the stop button appears and the start button disappears. Same goes for the stop button. However, if the app is killed after being in the background for some time, the start button reappears even though I never pressed stop in the first place. – user1871869 Jun 01 '15 at 23:05
  • You need to save state as needed. Typically when an app goes to the background you save any state incase the app is killed. – rmaddy Jun 01 '15 at 23:06
  • @rmaddy ah thanks for your help. So would I just need to return true on the app delegate methods `shouldSaveApplicationState` and `shouldRestoreApplicationState`? – user1871869 Jun 01 '15 at 23:11

1 Answers1

0

If you want to do stuff in the background you should look into background tasks.

Msencenb
  • 5,675
  • 11
  • 52
  • 84