When the app receives a low memory warning message, 3 situations can happen :
- your app has just been launched and the user has not done anything special
- the app is running and there is a current context
- the app is in the background with some running context
So when you receive this message, your are supposed to free memory... But where ? And how ?
I understand that :
initWith
..... must set the default static values.viewDidLoad
must load any non static objectdidReceiveMemoryWarning
must free those non static objects- I don't see what can/must be done in
viewDidUnload
...
I guess some retained values must be set to nil somewhere... in didReceiveMemoryWarning ?
And what must be done with the active context (positions of thing of the screen, displayed text, ...) so when viewDidLoad is called again, those thing appear again as they where before the memoryWarning call ?
I mean, imagine 2 scenarios :
Scenario 1
- you are working on something... you wrote some text in a field, did not saved it, opened another view, moved a view on the screen.
- You send the app in background.
- Then a memoryWarning is sent to the app.
- After that, the user send the app to foreground : it should display things like they was on exit, on the current view as on previous views, but if everything has been released, how may you do this ?
Scenario 2
- you are working on something... you wrote some text in a field, did not saved it, opened another view, moved a view on the screen.
- Then a memoryWarning is sent to the app.
- You don't want to loose what is on the view, nor what was on the previous view. You don't want neither the screen to flicker because of a release / reload feature. How do you deal with this ?
So when those memory warning happens, do you have any other choice than writing things to disk to display them back later ?
And when do you load those again ? I have a viewController that loads (viewDidLoad), receive a memoryWarning, unloads (viewDidUnload), but when going back to it, viewDidLoad is not called again ? Do this must be done in viewWillAppear ? Do I have to think that anytime viewWillAppear is triggered, I can assume that things that are supposed to be displayed on it are loaded ?
Any help, even with valuable links, would be great !
Thank you for your help.
Consider the alternatives to your scenarios. Your app could be killed if it does not free enough memory, which would be even more jarring to the user. One might choose potentially flickering the current display over losing the user's valuable data.