-1

After reading some recommendations on stack overflow and some other articles I have implemented a singleton class that allows me to call a method to update a user's current location. To avoid continuous update of the location information, I added a StopUpdatingPosition call. This works fine for the first time, however following calls fail.

I believe this is because of the way the geolocation stuff is initiated in the examples provided where it checks to see if the singleton is already in place(?)

If a singleton is initialised does it remain in memory? On subsequent calls can I check to see if this is the case and just request it to start updating position?

Here is what I have tried so far.

- (id)init {
    self = [super init];

    if(self) {
        self.locationManager = [CLLocationManager new];
        [self.locationManager setDelegate:self];
        [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
        [self.locationManager setHeadingFilter:kCLHeadingFilterNone];
        [self.locationManager startUpdatingLocation];
        geocoder = [[CLGeocoder alloc] init];
    }

    return self;
}

+ (LocationFunction*)sharedSingleton {
    static LocationFunction* sharedSingleton;
    if(!sharedSingleton) {
        @synchronized(sharedSingleton) {
            sharedSingleton = [LocationFunction new];
        }
    }

    return sharedSingleton;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        myLat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
        myLong = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    }

    // Store the Long & Lat to global variables here
    NSLog(@"Resolving the Address");


        } else {
    // Handle location lookup fail here
        }
    } ];
    [manager stopUpdatingLocation];
    }

I am looking for a way of calling method in an external class from different view controllers. This method obtains the user's current position (Long & Lat), records the info in a global variable, stops any requests to update location method and notifies the view controller when it is done.

Pointers appreciated.

Andy897
  • 6,915
  • 11
  • 51
  • 86
Taylermade
  • 11
  • 3
  • 1
    What examples are you talking about at different places in the post. Care to give the links ? – Andy897 Jul 30 '14 at 07:27
  • The delegate method that you are using is deprecated - refer to CLLocationManagerDelegate reference. I would remove the calls to start/stop updating location and provide instance methods on your singleton class that do this. You can then call these as required. The singleton will remain in memory as it holds a reference to itself. – Paulw11 Jul 30 '14 at 08:09
  • @Paulw11 Thanks. I'll take a look at what you're suggesting. – Taylermade Jul 30 '14 at 16:35
  • @Andy897 I'm afraid I don't have them to hand. – Taylermade Jul 30 '14 at 16:36

1 Answers1

0

Another stackoverflow question has an answer that provides a solution. Note however that the call is not correct (I am unable to comment on the answer - too new to stackoverflow!). It should reference "sharedManager" :

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[MyClass sharedManager] startCapture];
}

Hope this helps anyone searching.

Community
  • 1
  • 1
Taylermade
  • 11
  • 3