2014-01-27 114 views
0

我有一個駕駛應用程序,當我開始旅行時,我必須收集位置詳細信息以及日期和時間(以小時,分鐘,秒和毫秒爲單位)以及加速計詳細信息以及日期和時間(以小時,分鐘,每0.25秒秒和毫秒,當行程停止時應停止記錄。我已用於位置細節的定時器間隔1秒問題與計時器

self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                target:self 
                selector:@selector(locationTimerFired:) 
                userInfo:nil 
                repeats:YES]; 

- (void) locationTimerFired:(NSTimer *)timer 
    { 
     CLLocation *newLocation = locationManager.location; 

     [self updateLocation:newLocation andSpeed:newLocation.speed]; 
     [defaultCenter postNotificationName:LocationChangedNotification 
                 object:nil 
                 userInfo: userInfo]; 


} 

在接收LocationChangedNotification我記錄的位置和如所提到的,每第二時間。

當應用程序處於前臺時,但應用程序處於後臺時,某些時候罰款定時器計時器觸發間隔不準確,它有時會觸發2秒,持續3秒,有時甚至每秒觸發2次爲什麼會發生這種情況? 請建議。此外,我還在info.plist中以後臺模式註冊了位置更新。

我已經採取了計時器與間隔0.25秒加速度計細節如下

self.accelTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 
                 target:self 
                 selector:@selector(timerFired:) 
                 userInfo:nil 
                 repeats:YES]; 

感謝。

+0

這不是你問過的同一個問題嗎? http://stackoverflow.com/questions/21279771/scheduledtimerwithtimeinterval-is-not-calling-at-correct-intervals-sometimes – Gad

+0

也許最好是依靠背景而不是定時器的重要運動。是否真的需要在同一地理位置每秒鐘啓動計時器? – malex

回答

0

請勿使用計時器。相反,請配置位置管理器,以期望的準確度給出回調(推測最大),並使用回調來保存當前時間的位置和速度。

然後,當您開始使用/顯示數據時,您可以過濾/外推以獲取所需時間間隔的位置數據。

定時器不能在後臺工作,因爲它們不是爲了設計。位置回調被設計爲...