每隔X分钟获取iPhone位置

时间:2020-12-27 02:26:46

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

我不能通过主要的位置更改,因为它必须检查每X分钟,无论他们是否已移动。

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并暂时休眠该线程,问题是它睡在前端所以用户无法点击任何东西。 (这必须在应用程序未运行时运行)

[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]);
}

2 个解决方案

#1


2  

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.

不要这样做......我的意思是睡眠部分。 “无论他们是否已经移动”部分,您都可以手动处理。只是不要使用CLLocationManager事件来触发业务逻辑。保存位置,然后随时查看。如果它没有改变,那么你的逻辑没有任何改变,因为你无论如何都需要它。

- (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

您可以使用NSTimer每隔X分钟报告该属性上的任何内容。使用“主要更改”来节省电池电量

#2


1  

Instead of sleeping the NSThread, you can use NSTimer

您可以使用NSTimer,而不是睡眠NSThread

-(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];
}

#1


2  

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.

不要这样做......我的意思是睡眠部分。 “无论他们是否已经移动”部分,您都可以手动处理。只是不要使用CLLocationManager事件来触发业务逻辑。保存位置,然后随时查看。如果它没有改变,那么你的逻辑没有任何改变,因为你无论如何都需要它。

- (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

您可以使用NSTimer每隔X分钟报告该属性上的任何内容。使用“主要更改”来节省电池电量

#2


1  

Instead of sleeping the NSThread, you can use NSTimer

您可以使用NSTimer,而不是睡眠NSThread

-(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];
}