2011-10-04 75 views
0

我想在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。

回答

0

我認爲你應該做的是 - 設定,當你調用[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,

阿克沙伊

相關問題