Sean Danzeiser
Sean Danzeiser

Reputation: 9243

CLLocationManager startUpdatingLocation causing a lag

I've noticed that in my app, when I call startUpdatingLocation, my UI locks up briefly while i presume the device gathers the location data. Is this typical and is there any way to limit this? heres what my code looks like

    -(void)getUserLocation {
    if (!locationManager) 
    {
        locationManager = [[CLLocationManager alloc]init];
    }

    [locationManager setDelegate:self];
    [locationManager startUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    self.userCoordinate = newLocation.coordinate;

    NSLog(@"user coordinate in location manager: %f,%f",self.userCoordinate.latitude,self.userCoordinate.longitude);
    [locationManager stopUpdatingLocation];
    locationManager = nil;

    userLocationAvailable = YES;
    [self getDataForScrollView];

}

any ideas? Thanks!

Upvotes: 0

Views: 1289

Answers (2)

Dean Davids
Dean Davids

Reputation: 4214

You may benefit from looking at my use of the location services in the background. Essentially, I set up the location manager in main thread but all the processing on receipt of the location events is done on background thread.

That will allow your main thread to continue without blocking.

See code here TTLocationHandler GitHub Repository

Upvotes: 0

nluo
nluo

Reputation: 2147

That's because you are getting the location in the main thread, whereas the main thread is used to responsible for UI stuff . i.e. user touch events. that's why u feel your app has a bit of lag.

That's quite normal if you do a large task ( e.g. get data from web server) in main thread. You could put a "Loading" spin view to indicate the user instead of make your UI frozen.

Alternatively, you could try to get the location in another thread . I was trying to do this too but i have not succeeded yet. So i am currently doing with the first approach.

Upvotes: 1

Related Questions