Memory and life cycle of passed variables
There is one thing I don't manage to get a good understanding of and it is holding me back when I try to optimise my apps. Better is to give you an exemple of the common situation I am dealing with.
I use a UINavigationController for most of my apps. The root VC will set some vars depending of localisation and Internet Connexion and start stacking up VCs.
The first VC (after the root one) will used the var passed by the root VC to set some other vars that it will pass in turn to another VCs that it will ask the rootVC to push onto the stack. That works fine.
Now imagine that the second VC takes quite a bit of memory which make the app to unload the first VC, now if I come back from the second VC to the first one, the first VC load again but has lost the data that the rootVC had passed on to it the first time it stacked it up.
Hopping that I made myself clear, what is the best approach to avoid this kind of problem that lead fataly to an application crash ?
Should I implement the viewWillAppear Methods of each VC to check the existance of the data that I need, but then what ? used a notification to ask the precedent VC to pass the data back again ? From whom ? That sounds a lot of tedious works and not a very helpful infrastructure.
My second thought was to store on the root VC object all the important data that I would need and use notification to send them back on demand (as I proposed before) but then what garantie have I got that this rootVC will not be unloaded.
I am sure 90% of anyone's application needs to pass on data from views to views, so from VC to VC, even so you don't use UINavigationController, how do you deal with the problem of keeping essential data when memory is required.
If at least I could be sure that the object responsable for the UINAvigation COntroller is never unloaded, I would start thinking working on a class that work upon notification to pass data back and forth but am I going in the right direction there ?
If you had any suggestion I would greatly appreciated.
|