【问题标题】:NSTimer and NSRunLoopNSTimer 和 NSRunLoop
【发布时间】:2012-07-26 01:27:45
【问题描述】:

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

NSTimer 工作的唯一方法是:

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

然后删除它:

[userStoppedMovingTimer invalidate];

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

【问题讨论】:

  • 如果您使用+scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 创建计时器,这应该不是问题。

标签: objective-c nstimer cllocationmanager nsrunloop


【解决方案1】:

来自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: 方法。 (创建后,您必须手动将计时器添加到运行循环中 调用对应NSRunLoopaddTimer:forMode:方法 对象。)

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

【讨论】:

  • 嗨@Tom,就我而言,我使用了运行循环,但是当我在iOS设备中安装我的应用程序时它不起作用?那你有什么解决办法吗?
猜你喜欢
  • 2013-03-28
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多