Shouldn't we use ViewDidLoad to initialize locationManager?


#1

Apple documentation (UIViewController Class Reference - in the discussion of initWithNibName:bundle:) mentions -

“The nib file you specify is not loaded right away. It is loaded the first time the view controller’s view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there.”

Based on the above statement, should we use ViewDidLoad to initialize locationManager. Like the following:

[code]- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}

  • (void)viewDidLoad
    {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:50];
    [locationManager setDelegate:self];
    [locationManager startMonitoringSignificantLocationChanges];
    }
    [/code]

#2

The CLLocationManager itself isn’t dealing with the views. It doesn’t matter if the view is loaded or not, it’s going to start finding the location. If the location manager finds a location before the view is loaded, that’s ok - the IBOutlets will be nil in the locationManager:didUpdateToLocation:fromLocation: method and so no harm. Might as well start it as soon as you know you need a location. Could you start it in viewDidLoad? Absolutely, wouldn’t hurt anything minus potentially delaying finding the user location.