I have an UIViewController
subclass where I have an UITableView
. This is the way I use viewDidLoad
& viewWillAppear
so far.
-(void)viewDidLoad
{
//Setup my datasource
//Setup my views, tableviews, constraints
}
-(void)viewWillAppear:(BOOL)animated
{
//Setup my datasource
}
viewDidLoad
will be called only once when the view is constructed. viewWillAppear
will be called everytime I visit the view controller.
Here, why I setup my datasource(NSArray
) in both places is, whenever I come into the viewcontroller, I just need to reconstruct the datasource array.
I can simply do like this.
-(void)viewDidLoad
{
//Setup my views, tableviews, constraints
}
-(void)viewWillAppear:(BOOL)animated
{
//Setup my datasource
//Reload tableview
}
But, This feels bit slow in showing data when the view controller appears.
Questions:
What is the best practice to setup the datasource array? I don't want a delay by setting it only in viewWillAppear
. Or should I setup the datasource like this?
-(void)viewDidLoad
{
//Setup my datasource
//Setup my views, tableviews, constraints
}
-(void)viewWillAppear:(BOOL)animated
{
//Setup my datasource
//Reload tableview
}
If YES, for the first time, I need to calculate the datasource array two times.
To overcome this issue, I need to keep a BOOL
value like isFirstTime (stored in NSUserDefaults
) by setting it in viewDidLoad
and check it in viewWillAppear
method as like this:
-(void)viewDidLoad
{
//Set isFirstTime as YES
//Setup my datasource
//Setup my views, tableviews, constraints
}
-(void)viewWillAppear:(BOOL)animated
{
if(isFirstTime)
{
//Skip
isFirstTime = NO;
}
else
{
//Setup my datasource
//Reload tableview
}
}
Should I really do this much complex things to achieve this? Suggestions needed!!
Thanks