If I stop using my app for a long time then go back to it, it seems like the viewDidLoad method is being called again, even though I never “exited” the app (by pressing the home key twice and tapping the X) nor did I reboot the iPhone. If I just go to the home screen and then open the app again after a short time this does not happen. What is going on?
If I stop using my app for a long time then go back to
Share
This has to do with the way that the operating system manages memory and how it deals with having many different apps open at one time. In a short summary, if your app is in the background for a long period of time, eventually, the OS will decide that it is inactive, and the memory associated with its view will be marked for reuse somewhere else where it is more needed. This is the
viewDidUnloadmethod in your view controller. Any time theviewDidUnloadmethod is called, the viewDidLoad method will be called again so that you get a chance to reload your view before the user sees it.Edit:
You cannot rely on this phenomenon happening every
xminutes. It will only happen as the OS requires more memory for active apps. If you want to make sure the user always gets updated information when he or she resumes usage of your app, use NSNotificationCenter and register for theUIApplicationDidBecomeActivenotification.