Where to initialize data structures: init or viewDidLoad?

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

Also, for both cases, where should I free them: dealloc , viewDidUnload or didReceiveMemoryWarning ?

+4
source share
1 answer

Comprehensive answer: "It depends on the life cycle of your presentation." Each method has its own place regarding how your views will load and appear.

However, as a rule, viewDidLoad is a good default place to go first - your XIB views are already created, so you can set their properties. If you load and unload views a lot, you may need to pull clean data, which is pretty static for your init calls.

viewWillAppear is another option, depending on the life cycles of your views and how much your data may change while the view is hidden.

ETA: Release your memory in additional calls to where you allocated it. In dealloc , viewDidUnload or viewDidDisappear . didReceiveMemoryWarning should always unload anything, no matter where you initialized it, and your code should be smart enough to reload data as soon as it is active.

+3
source

Source: https://habr.com/ru/post/1391060/


All Articles