6

我的应用程序使用CLLocationManager. 在代表电话会议中didUpdateToLocation,我做了所有有趣的事情来保存他们的位置。但是,我需要一种方法来测试它们是否已经停止。那样我就可以停止记录位置并考虑他们的旅行。所以我有一个每次调用时都会添加和删除的NSTimerin 。当用户停止并停止被调用时,它将被启动。CCLocationManagerdidUpdateToLocationCLLocationManager

我能得到NSTimer工作的唯一方法是:

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

然后删除它:

[userStoppedMovingTimer invalidate];

过去我从来不需要添加这样的计时器。有人可以解释为什么会这样吗?

4

1 回答 1

9

文档中:

创建定时器的三种方法:

  1. 使用scheduledTimerWithTimeInterval:invocation:repeats:or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:类方法创建计时器并在默认模式下将其安排在当前运行循环中。

  2. 使用timerWithTimeInterval:invocation:repeats:or timerWithTimeInterval:target:selector:userInfo:repeats:类方法创建计时器对象,而不将其安排在运行循环中。addTimer:forMode:(创建完成后,你必须通过调用对应的NSRunLoop对象的方法,手动将定时器添加到运行循环中。)

  3. 分配计时器并使用 initWithFireDate:interval:target:selector:userInfo:repeats:方法对其进行初始化。(创建完成后,必须通过调用addTimer:forMode:相应NSRunLoop 对象的方法手动将定时器添加到运行循环中。)

您之前可能使用的是选项 1,现在您使用的是选项 2 或 3。

于 2012-07-26T04:08:55.470 回答