gpt4 book ai didi

objective-c - NSTimer - 为什么 scheduledTimerWithTimeInterval 工作,而 initWithFireDate 不工作?

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:21 24 4
gpt4 key购买 nike

这会根据需要每 60 秒重复调用我的选择器:

autoDeleteTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:[SimpleDB class] selector:@selector(autoDelete:) userInfo:nil repeats:YES];

下一行根本没有调用它。不是最初也不是 60 秒后:

autoDeleteTimer = [[NSTimer alloc] initWithFireDate: [NSDate dateWithTimeIntervalSinceNow:1] interval:60 target:[SimpleDB class] selector:@selector(autoDelete:) userInfo:nil repeats:YES];

谁能解释一下为什么?谢谢。

最佳答案

您需要将第二个计时器添加到主循环中:

[[NSRunLoop mainRunLoop] addTimer: autoDeleteTimer forMode:NSDefaultRunLoopMode];

来自方法的文档:

- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

Return Value: The receiver, initialized such that, when added to a run loop, it will fire at date and then, if repeats is YES, every seconds after that.

You must add the new timer to a run loop, using addTimer:forMode:. Upon firing, the timer sends the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)

NSTimer Apple Doc

关于objective-c - NSTimer - 为什么 scheduledTimerWithTimeInterval 工作,而 initWithFireDate 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19405069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com