Search code examples
iphoneobjective-cnstimerviewdidload

NStimer starting on view did load, giving false times! objective C iPhone


I am starting a timer with view did load

- (void)viewDidLoad
{
[super viewDidLoad];
locationManager.delegate = self;
[NSTimer scheduledTimerWithTimeInterval: 10.0 //interval here must be a float
target: self
selector:@selector(onTick:)
userInfo: nil repeats:YES];
}

then i have my timer method:

-(void)onTick:(NSTimer *)timer {
[locationManager startUpdatingLocation];

}

then the location manager is called since its a delegate:

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"made it to location manager");

NSTimeInterval timePassedSinceLastLocationUpdate = fabs([oldLocation.timestamp timeIntervalSinceNow]);

NSLog(@"update time: %f", timePassedSinceLastLocationUpdate);

NSLog(@"--------------");
//other stuff happens
[locationManager stopUpdatingLocation];
}

However, the NSLOG that is shown is this:


2012-07-31 13:02:28.092 round5[1713:f803] made it to location manager
2012-07-31 13:02:28.095 round5[1713:f803] update time: 0.000000
2012-07-31 13:02:28.095 round5[1713:f803] --------------
2012-07-31 13:02:33.298 round5[1713:f803] made it to location manager
2012-07-31 13:02:33.298 round5[1713:f803] update time: 5.222202
2012-07-31 13:02:33.300 round5[1713:f803] --------------
2012-07-31 13:02:44.086 round5[1713:f803] made it to location manager
2012-07-31 13:02:44.086 round5[1713:f803] update time: 15.986345
2012-07-31 13:02:44.087 round5[1713:f803] --------------
2012-07-31 13:02:53.297 round5[1713:f803] made it to location manager
2012-07-31 13:02:53.302 round5[1713:f803] update time: 9.217123
2012-07-31 13:02:53.303 round5[1713:f803] --------------
2012-07-31 13:03:04.096 round5[1713:f803] made it to location manager
2012-07-31 13:03:04.097 round5[1713:f803] update time: 20.007919
2012-07-31 13:03:04.098 round5[1713:f803] --------------
2012-07-31 13:03:13.297 round5[1713:f803] made it to location manager
2012-07-31 13:03:13.298 round5[1713:f803] update time: 9.202388
2012-07-31 13:03:13.300 round5[1713:f803] --------------
2012-07-31 13:03:24.111 round5[1713:f803] made it to location manager
2012-07-31 13:03:24.112 round5[1713:f803] update time: 20.012550
2012-07-31 13:03:24.113 round5[1713:f803] --------------

as you can see towards the bottom the pattern normalizes and it either reads ~10 seconds (which is right, because thats what the interval is set to and compared against the system clock)

and 20... which...i don't know where it comes from.

I guess my question is "Why isn't it reading 10 seconds all the time; why does it double the NStimer value?"

thanks a lot!


Solution

  • You have two mistakes here. First, you shouldn't call startUpdatingLocation repeatedly. You should call it one time to start requesting updates as they're available (you should then call stopUpdatingLocation when you don't need updates anymore). Your second mistake is believing that calling startUpdatingLocation will always return you a new location. You're not checking the last time your NSTimer ran. You're checking the last time CLLocationManager updated the location (oldLocation.timestamp). This can be any arbitrary amount of time. It may not have been able to get an update when you asked for it last time. It may have updated at times you didn't ask for it.

    Read over the Location Awareness Programming Guide. It explains how to request updates and when to request updates. Generally you turn it on and tell the system how accurate you want your updates. You then let the system call you when things change. You do not repeatedly ask for location updates.