ios NSTimer 不触发选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9918408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSTimer not firing the selector
提问by inforeqd
In ios5.0 with ARC, in my rootviewcontroller I call a method in a security manager object that is held by the app delegate. In that method I setup the timer as below
在带有 ARC 的 ios5.0 中,在我的 rootviewcontroller 中,我调用了由应用程序委托持有的安全管理器对象中的方法。在那种方法中,我设置了定时器如下
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self
selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
However, this never fires the selector ie. updateModel: never gets called. What may be wrong? Is there another more efficient way I can do this without using NStimer?
但是,这永远不会触发选择器即。updateModel:永远不会被调用。可能有什么问题?有没有另一种更有效的方法可以在不使用 NStimer 的情况下做到这一点?
采纳答案by sosborn
You seem to be a bit mixed up with your timer variable.
您似乎与您的计时器变量有点混淆。
You initialize a new timer but you aren't actually using it. Do you want to use the timer you initialized or do you want to you ApplicationDelegate.timer?
您初始化了一个新计时器,但实际上并没有使用它。你想使用你初始化的计时器还是你想要 ApplicationDelegate.timer?
Here are the two possible solutions.
以下是两种可能的解决方案。
Option One (assuming that you have a class instance titled ApplicationDelegate and that it has a timer property):
选项一(假设您有一个名为 ApplicationDelegate 的类实例并且它有一个 timer 属性):
ApplicationDelegate.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:ApplicationDelegate.timer forMode:NSRunLoopCommonModes];
Option Two:
选项二:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
回答by tmanthey
Could also be a threading problem:
也可能是线程问题:
if
如果
[NSThread isMainThread]
is false then start the timer like this:
为假然后像这样启动计时器:
dispatch_async(dispatch_get_main_queue(), ^{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
})
回答by WINSergey
I catch the same issue and I fire timer in main queue to solve it:
我遇到了同样的问题,我在主队列中启动计时器来解决它:
[NSURLConnection sendAsynchronousRequest:request queue:_operationQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
[self loopUpUpdateStart];
}];
-(void)loopUpUpdateStart{
dispatch_async(dispatch_get_main_queue(), ^{
_loopTimerForUpRevision =
NSTimer scheduledTimerWithTimeInterval: kNetworkLoopIntervalUpRev
target: self
selector: @selector(myCoolMethod)
userInfo: nil
repeats: YES];
TRACE(@"Start Up updates");
});
}
回答by Rob Napier
This line has several problems:
这一行有几个问题:
[[NSRunLoop currentRunLoop] addTimer:ApplicationDelegate.timer forMode:NSRunLoopCommonModes];
First, it should not be required at all. -scheduledTimerWithTimeInterval:...
already adds the timer to the runloop. You do not need to add it again.
首先,它根本不需要。-scheduledTimerWithTimeInterval:...
已经将计时器添加到运行循环中。您不需要再次添加它。
Second, the local variable timer
is unrelated to the property ApplicationDelegate.timer
(which is presumably nil
at this point).
其次,局部变量timer
与属性无关ApplicationDelegate.timer
(大概是nil
此时)。
If you're talking to the application delegate so much that you've created something called ApplicationDelegate
(a global? a macro?), you're talking to it too much. The application delegate is the delegate for the application; it assists in the application starting and stopping and responding to system events. The application delegate is not a place to store global variables. A timer is definitely not the kind of thing you'd fetch from another object in any case.
如果您与应用程序委托进行了过多的交流,以至于您已经创建了一个名为ApplicationDelegate
(全局?宏?)的东西,那么您与它的交流就太多了。应用委托是应用的委托;它协助应用程序启动和停止以及响应系统事件。应用程序委托不是存储全局变量的地方。无论如何,计时器绝对不是您从另一个对象中获取的那种东西。