2012-07-26 84 views
6

我的應用使用CLLocationManager跟蹤用戶。在代表電話didUpdateToLocation我做所有保存他們的立場有趣的東西。但是,我需要一種方法來測試它們是否停止。那我可以停止錄製地點,並考慮他們的旅行結束。所以我在CCLocationManager中有一個NSTimer,每調用一次didUpdateToLocation就會被添加和刪除。當用戶停止並且CLLocationManager停止呼叫時,它將被啓動。NSTimer和NSRunLoop

我所能得到的NSTimer工作的唯一方法就是要做到:

[[NSRunLoop mainRunLoop] addTimer:userStoppedMovingTimer forMode:NSRunLoopCommonModes]; 

然後將其刪除:

[userStoppedMovingTimer invalidate]; 

我從來沒有在添加計時器這樣過去。有人可以解釋爲什麼這是什麼?

+0

如果使用'+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:'創建計時器,則這不應該成爲問題。 – 2012-07-26 01:42:56

回答

8

documentation

有三種方法來創建一個定時器:

  1. 使用scheduledTimerWithTimeInterval:invocation:repeats:scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:類 方法來創建計時器,並安排它在當前運行在 中循環默認模式。

  2. 使用timerWithTimeInterval:invocation:repeats:timerWithTimeInterval:target:selector:userInfo:repeats:類方法 而不調度它在運行循環來創建計時器對象。 ( 創建後,必須手動調用 相應NSRunLoop對象的addTimer:forMode:方法中添加計時器到運行迴路。)

  3. 分配定時器並使用所述 initWithFireDate:interval:target:selector:userInfo:repeats:方法初始化。 (創建後,您必須定時手動 調用相應的NSRunLoop 對象的addTimer:forMode:方法添加到一個運行循環。)

你在利用可能的選項1之前,現在你'使用選項2或3.