当LocationManager中的委托需要太长时间时显示UIAlert

问题描述:

我想在LocationManager花费太长时间时创建警报。在其委托的方法,我检查到newLocation的时间戳,以确保它的东西,最近的:当LocationManager中的委托需要太长时间时显示UIAlert

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSDate *eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (abs(howRecent) < 1.0) {  // process time if time is less than 1.0 seconds old. 
     timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCount:) userInfo:nil repeats:YES]; 
     return; 
    } 

一些调试之后,我认识到,把计时器,如果块是不正确的。最终发生的事情是,定时器永远不会失效,或者更确切地说我的updateCount:方法继续运行。我认为发生的事情是代表不断地接到一个不好的时间,然后继续运行更多的定时器,所以我在UIAlert之后得到UIAlert。

我在测试项目中使用了相同的updateCount:方法来创建一个30秒的计时器并使其失效,并且它工作正常。然而,现在我陷入困境,因为我基本上想要找到这个人的位置,但是如果它需要很长的时间(> 30秒),请发出警报。我不确定我应该把这种代码放在哪里。在我看来,把它放在我正在检查时间戳的委托中,因为这是我期望跟踪的错误状态。但是这对我来说似乎并不好。

有没有更好的地方可以进行这种检查?还是有更好的方法来完成这种任务?我对编程相当陌生,所以我的知识是有限的。 TIA。

我认为你应该做的是 - 设定,当你调用[locationManager startUpdatingLocation];现在30秒计时器什么,你可以更新你的方法看起来喜欢 -

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSDate *eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (abs(howRecent) < 1.0) {  //good time found 
     //use this location 
     //either remove the 30 second timer or set a boolean 
     self.goodLocationFound = YES; 
     [locationManager stopUpdatingLocation]; 
    } 

在你的计时器的方法,你可以做 -

{ 
     [locationManager stopUpdatingLocation]; 
     if(self.goodLocationFound == NO) 
     { 
       //display alert 
     } 
} 

HTH,

阿克沙伊