当应用程序在后台时,人们如何处理预定的NSTimer?
假设我每小时在我的应用中更新一些内容.
updateTimer = [NSTimer scheduledTimerWithTimeInterval:60.0*60.0
target:self
selector:@selector(updateStuff)
userInfo:nil
repeats:YES];
Run Code Online (Sandbox Code Playgroud)
在后台时,此计时器显然不会触发(?).当用户回到应用程序时应该怎么办?计时器是否仍然在运行,同一时间?
如果用户在一小时内回来,会发生什么.它会在错过的所有时间触发,还是等到下一次更新时间?
我想要它做的是在应用程序进入前台后立即更新,如果它应该触发的日期是过去的.那可能吗?
我用的NSTimer是这样的:
timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(tick) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
当然,NSTimer保留了创建保留周期的目标.此外,self不是一个UIViewController所以我没有任何东西viewDidUnload,我可以无效的计时器来打破循环.所以我想知道我是否可以使用弱引用:
__weak id weakSelf = self;
timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:weakSelf selector:@selector(tick) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
我听说计时器必须无效(我想从运行循环中释放它).但我们可以在我们的dealloc中做到这一点,对吗?
- (void) dealloc {
[timer invalidate];
}
Run Code Online (Sandbox Code Playgroud)
这是一个可行的选择吗?我已经看到人们处理这个问题的方法很多,但我还没有看到这个.
可能重复:
NSTimer不会停止
在我的应用程序中,我使用NStimer每3秒调用一次动画函数.我想在计时器仍在运行时停止此计时器并调用另一个事件.这可能吗?
我一直在使用选择器,甚至在迁移到Swift之后我能够毫无问题地使用它们.这就是我在Swift 2上使用的方式没有问题,直到我将Xcode更新到7.3版:
正如使用可以看到我使用选择器与NSTimer.
这是被称为的动作:
func Start () {
}
Run Code Online (Sandbox Code Playgroud)
如您所见,Xcode 7.3现在发出警告"没有用Objective-C选择器声明的方法".通过单击警告,Xcode通过添加"Selector"快速修复代码,但我仍然得到相同的警告:
我需要在应用程序启动时调用方法,然后每隔5秒调用相同的方法.
我的代码非常简单:
// Call the method right away
[self updatestuff:nil]
// Set up a timer so the method is called every 5 seconds
timer = [NSTimer scheduledTimerWithTimeInterval: 5
target: self
selector: @selector(updatestuff:)
userInfo: nil
repeats: YES];
Run Code Online (Sandbox Code Playgroud)
是否有计时器的选项,所以它立即触发,然后每5秒触发一次?
我希望能够在将来安排三个小事件,而不必为每个事件编写一个函数.我怎么能用这个NSTimer呢?我理解块有助于匿名功能,但它们可以在内部使用NSTimer,如果是,如何使用?
[NSTimer scheduledTimerWithTimeInterval:gameInterval
target:self selector:@selector(/* I simply want to update a label here */)
userInfo:nil repeats:NO];
Run Code Online (Sandbox Code Playgroud) 在这种情况下,永远不会调用timerFunc().我错过了什么?
class AppDelegate: NSObject, NSApplicationDelegate {
var myTimer: NSTimer? = nil
func timerFunc() {
println("timerFunc()")
}
func applicationDidFinishLaunching(aNotification: NSNotification?) {
myTimer = NSTimer(timeInterval: 5.0, target: self, selector:"timerFunc", userInfo: nil, repeats: true)
}
}
Run Code Online (Sandbox Code Playgroud) 如何阻止我的计时器运行?不是暂停,而是停顿.
import UIKit
class LastManStandingViewController: UIViewController {
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var timeTextbox: UITextField!
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var stopButton: UIButton!
var myCounter = 0
var myTimer : NSTimer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
timeLabel.text = String(myCounter)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startTimer(){
myTimer = …Run Code Online (Sandbox Code Playgroud) 我正试图用NSTimer制作秒表.
我给出了以下代码:
nst_Timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showTime) userInfo:nil repeats:NO];
Run Code Online (Sandbox Code Playgroud)
并且它在几毫秒内无法工作.它需要超过1毫秒.
当我调用scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:主线程并将时间间隔设置为5秒时,下面的代码被执行,并且在5秒后调用定时器选择器.
但是如果我在某个后台线程中尝试相同,下面的代码scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:将不会被执行,它将等待定时器触发然后执行.当然,为了在后台线程中运行计时器,我首先得到了一个实例NSRunLoop并运行它.
有没有办法在后台线程中设置定时器并使其无阻塞,所以代码在它立即执行后?
nstimer ×10
ios ×4
iphone ×4
objective-c ×4
swift ×3
ios5 ×1
nsdate ×1
nsthread ×1
retain-cycle ×1
timer ×1