stopUpdatingLocation doesn't work


#1

Hello,

as I was trying to complete the challenge Reverse Geocoding, the text wasn’t added in the label.
I added some breakpoints to Xcode and saw that even though [locationManager stopUpdatingLocation] is executed and [locationManager startUpdatingLocation] is not executed again, it keeps getting updated locations and going through the didUpdateToLocation delegate.
I’ve been searching about it on the internet and lots of people are saying “stopUpdatingLocation doesn’t work” but can’t find out why.

Here’s some narrowed-down code:
It goes once through breakpoint 1, then 3, 2, 3, 2, 3, 2 and keeps going between 3 and 2

  • (BOOL)textFieldShouldReturn:(UITextField *)tf
    {
    [self findLocation];
    [tf resignFirstResponder];
    return YES;
    }

  • (void)findLocation
    {
    [locationManager startUpdatingLocation]; <<<<< BREAKPOINT 1
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
    }

  • (void)foundLocation
    {
    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation]; <<<<<<< BREAKPOINT 2
    }

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation); <<<<<<<<<<< BREAKPOINT 3
[self foundLocation];
}


#2

Note it doesn’t go through
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@“Could not find location: %@”, error);
}


#3

It will continue to track locations because map view constantly updates user’s location when it is told to show it on the map. You can do this:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
	MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 250, 250);
	[worldView setRegion:region animated:YES];
	
	[worldView setUserTrackingMode:MKUserTrackingModeNone];
}

#4

Thanks for your reply, now it’s all more clear :slight_smile: