Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to initialize the data structures : init or viewDidLoad?

Which is the best place to initialize the data structures used to display content in my view controllers: init or viewDidLoad?

Also, for either case, where should I be releasing them: dealloc, viewDidUnload or didReceiveMemoryWarning ?

like image 796
NSExplorer Avatar asked Oct 09 '22 00:10

NSExplorer


1 Answers

The all-encompasing answer is, "It depends upon your view lifecycle." Each method has its own place relative to how your views will load and appear.

As a rule of thumb, though, viewDidLoad is a good default place to go first - your XIB views will already be instantiated so you can set their properties. If you do a lot of loading and unloading of views, you might want to push pure data that's fairly static back to your init calls.

viewWillAppear is another option, depending again upon your views' life cycles and how much your data is likely to change while the view is hidden.

ETA: Release your memory in the complementary calls to where you allocate it. In dealloc, viewDidUnload or viewDidDisappear. didReceiveMemoryWarning should always unload anything you can regardless of where you initialize it, and your code should be smart enough to reload the data once it is active again.

like image 117
Todd Masco Avatar answered Oct 13 '22 12:10

Todd Masco