Instantiating Singleton class with CLLocationManager in AppDelegate didFinishLaunchingWithOptions

StackOverflow https://stackoverflow.com/questions/16402201

  •  14-04-2022
  •  | 
  •  

Question

i am using a singleton class to track a users location. if i instantiate the class from within my running app and start tracking everything is peachy.

however, if the app is closed (in the app switcher so it isn't backgrounded any more) and restarted i check if there is an active event. in this case i instantiate the class and start the location tracking. the locationManager gets instantiated and the location arrow appears in the statusbar, but the

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
or 
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
never get called

any idea?

code singelton class:

@interface AlarmMeTrackUserLocation () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end

@implementation AlarmMeTrackUserLocation    
-(void)toggleLocationUpdates:(bool)toggle {

    if (!toggle) {
        [self.locationManager stopUpdatingLocation];
        self.locationManager = nil;
        sharedInstance = nil;
        NSLog(@"--- UserTrack Stopped ---");
        return;
    }


    if (!self.locationManager && toggle) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        self.locationManager.pausesLocationUpdatesAutomatically = YES;
        self.locationManager.distanceFilter = 200;
    }

    NSLog(@"--- UserTrack Started");
    [self.locationManager startUpdatingLocation];

}

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

- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error {
    ...
}

+ (AlarmMeTrackUserLocation *)sharedTrackUserLocation {

    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];

        //NSLog(@"--- New Instance created ---");

        return sharedInstance;
    }

    NSLog(@"--- Existing Instance Used ---");

    return sharedInstance;
}

code from AppDelegate:

@property (strong, nonatomic) AlarmMeTrackUserLocation *userTrack;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    self.userTrack = [AlarmMeTrackUserLocation sharedTrackUserLocation];
    self.userTrack.event = currentEvent;
    [self.userTrack toggleLocationUpdates:YES];

}
Was it helpful?

Solution

after carefully reviewing my code i could only see one difference: in the app delegate i start a backgrounding thread to fetch data from a webserver. if the request is complete the singleton class is instantiated and locationManager is set (still in the background thread).

after switching back to the main thread and instantiating its finally working!

so it seems you cant delegate a CLLocationManager from a backgrounding thread

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top