Johanisma
Johanisma

Reputation: 2058

CLLocation manager updates from background thread

I'm launching a localization request using Grand Central Dispatch :

- (void) findGroceriesNearMe {
    dispatch_queue_t downloadQueue = dispatch_queue_create("Groceries downloader", NULL);
    dispatch_async(downloadQueue, ^{
        CLLocationCoordinate2D userLocation = [LocationManagerController findMeWithCaller:self];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self userSuccessFullyFound:userLocation];
        });
    });
    dispatch_release(downloadQueue);
}

It calls a static method in my Singleton class LocationManager Controller :

+ (CLLocationCoordinate2D) findMeWithCaller: (UIViewController *) viewController {

    LocationManagerController *locationManagerController = [LocationManagerController locationManagerController];
    [locationManagerController startUpdates];

    while(![locationManagerController getterDone]){
        //mystique pour nous-- a approfondir
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

In the startUpdates method, the CLLocationManager, property of LocationManagerController, is initialized and asked to startUpdatingLocation.

Finally, the method when location updates happen :

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation  fromLocation:(CLLocation *)oldLocation
{
    locationDenied = NO;
    NSLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    // On vérifie que la newLocation est récente 

    if (abs(howRecent) > 10.0) {
        return;
    }

    // Test if it's not an invalid measurement

    if (newLocation.horizontalAccuracy < 0) return;

    // Test the measurement to see if it meets the desired accuracy 

    if (newLocation.horizontalAccuracy <= manager.desiredAccuracy) 

    {
        latitude = newLocation.coordinate.latitude;
        longitude = newLocation.coordinate.longitude;
        locationDefined = YES;
        [self setterDone:YES];
    }
}

My problem is that the locationManager only send 3 location updates and then stops sending updates even though I didn't ask it to stop. So basically, I never get out of the while(![locationManagerController getterDone]) loop.

By the way, before trying to implement this using GCD, it was working fine so I guess the issue has to do with my implementation of multi-threading.

Any idea ?

Edit

I don't get any error in the console. The program just keeps running but I'm stuck in that while loop and nothing else happens after the 3 first location updates.

Thanks !

Upvotes: 1

Views: 2363

Answers (2)

iFreeman
iFreeman

Reputation: 31

From CLLocationManager class reference:

Configuration of your location manager object must always occur on a thread with an active run loop, such as your application’s main thread.

Upvotes: 3

Umberto
Umberto

Reputation: 1421

A guess. If you are sitting at your desk and testing with your simulator the accuracy may not get better what you want

if (newLocation.horizontalAccuracy <= manager.desiredAccuracy) 

So you may get stuck in your loop. Try with higher accuracy while at your desk. Also consider if the accuracy is never better that what you want since it maybe that the gps reception is not good.

Let me know if that helps or if I was way off the mark :-)

-- Fasttouch

Upvotes: 0

Related Questions