user2034047
user2034047

Reputation: 23

Getting iPhone location every X minutes

I cant go by major location change because it has to check every X minutes regardless if they have moved.

This is how i'm attempting to do it now. I'm trying to run the locationManager in another thread and sleep that thread for the time, the problem is it sleeps the front end so the user cant tap anything. (This must run when the apps not running)

[locationManager performSelectorInBackground:@selector(startUpdatingLocation) withObject:nil];


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        [NSThread sleepForTimeInterval:10.0]; //600
        NSLog([NSString stringWithFormat:@"Longitude: %0.3f", newLocation.coordinate.longitude]);
        NSLog([NSString stringWithFormat:@"Latitude: %0.3f", newLocation.coordinate.latitude]);
}

Upvotes: 0

Views: 112

Answers (2)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Instead of sleeping the NSThread, you can use NSTimer

-(void)yourMethod{
   //your stuffs here

    if (/*once you are done*/) {
        [self.timer invalidate];
        self.timer=nil;
    }
}

-(IBAction)button:(id)sender;{

    self.timer=[NSTimer scheduledTimerWithTimeInterval:600.f target:self selector:@selector(yourMethod) userInfo:nil repeats:YES];
}

Upvotes: 1

Fernando Mazzon
Fernando Mazzon

Reputation: 3591

Don't do that... The sleep part I mean. The "regardless if they have moved" part you can take care of manually. Just don't use the CLLocationManager event to trigger your business logic. Save the location, then check it whenever you want. If it hasn't changed, well, nothing changes in your logic because you need it anyway.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   self.location = newLocation;
}

You can use an NSTimer to report whatever is on that property every X minutes. Use the "major changes" to save battery

Upvotes: 2

Related Questions