How to stop multiple times method calling of didUpdateLocations() in ios

前端 未结 10 1114
無奈伤痛
無奈伤痛 2020-11-28 06:45

This my code......

 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
 {

    location_updated = [locations lastObj         


        
相关标签:
10条回答
  • 2020-11-28 07:22

    for the time constraint, i did not understand code from accepted answer, posting a different approach. as Rob points out "When you first start location services, you may see it called multiple times". the code below acts on the first location, and ignores the updated locations for first 120 seconds. it is one way to address orginal question "How to stop multiple times method calling of didUpdateLocations".

    in .h file:

    @property(strong,nonatomic) CLLocation* firstLocation;
    

    in .m file:

    // is this the first location?
        CLLocation* newLocation = locations.lastObject;
        if (self.firstLocation) {
            // app already has a location
            NSTimeInterval locationAge = [newLocation.timestamp timeIntervalSinceDate:self.firstLocation.timestamp];
            NSLog(@"locationAge: %f",locationAge);
            if (locationAge < 120.0) {  // 120 is in seconds or milliseconds?
                return;
            }
        } else {
            self.firstLocation = newLocation;
        }
    
        // do something with location
    
    0 讨论(0)
  • 2020-11-28 07:24

    While allocating your LocationManager object you can set the distanceFilter property of the LocationManager. Distance filter property is a CLLocationDistance value which can be set to notify the location manager about the distance moved in meters. You can set the distance filter as follows:

    LocationManager *locationManger = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = 100.0; // Will notify the LocationManager every 100 meters
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    0 讨论(0)
  • 2020-11-28 07:29

    You could set a flag (Bool). When you instantiate your locationsManager set flag = true then when locationManager:didUpdateLocations returns inside a code block that you want to run only once set flag = false. This way it will only be run the once.

     if flag == true {
         flag = false
        ...some code probably network call you only want to run the once 
        }
    

    locations manager will be called multiple times but the code you want to execute only once, and I think that is what you are trying to achieve?

    0 讨论(0)
  • 2020-11-28 07:30

    I have similar situation. You can use dispatch_once:

    static dispatch_once_t predicate;
    
    - (void)update
    {
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
            [_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [_locationManager requestWhenInUseAuthorization];
        }
    
        _locationManager.delegate = self;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
        predicate = 0;
        [_locationManager startUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        [manager stopUpdatingLocation];
        manager = nil;
    
        dispatch_once(&predicate, ^{
            //your code here
        });
    }
    
    0 讨论(0)
提交回复
热议问题