I have a navigation based iPhone application. in the Middle of the application sometimes i am receiving the memory warning level 1 and 2. Suddenly all the values i fetched from database and created based on the user input in root viewcontroller including the values i saved in the nsuserdefaults losed totally. When i try come back to the root view controller i have empty table view without any data.
How to handle this issue, Please suggest any solution for this problem?
This is how it works, "unfortunately". When the
didReceiveMemoryWarningcomes in, your views are released and this explain why you don’t find any value in the table.What one should do is be prepared to create again all the views, according to Apple Docs.
If you want to implement those guidelines, a way to go is:
use
viewDidLoad/viewDidUnloadto "save/restore" your data;store your data in a model independent from your views;
in
viewDidUnload, called after a warning`, store in your controller enough information about the view state (i.e., if it is displaying data about a customer, his customer/ID), so that you can recreate that information by getting the data back from the model;in
viewDidLoad, called also when the view is restored after a memory warning, use the information that you saved in your view controller (if it is there), to recreate the view in the exact state where it was left (otherwise, put it in a default state).This should make it pretty easy.
One trick that you could deploy if it makes sense to you is the following:
override
-didReceiveMemoryWarningin your view controllers;for any view that you really don’t want to be automatically released, don’t call
[super didReceiveMemoryWarning]in your-didReceiveMemoryWarningoverride.Beware, this will make your life easier now, but will also defeat an important mechanism Apple has put in there to recover memory. This will not produce a crash in itself, but could make your device memory to fill up and your app be closed abruptly if you manage many views or your views are memory intensive (lots of graphics, etc.). So, your judgement here…