NSTimer 自动失效
当使用带有repeats:NO的定时器时,NSTimer需要多长时间才会自动失效。 运行泄漏分析器时这可能是错误警报吗?
当我发出服务器请求时,我会得到成功或失败。无论哪种方式,我都会创建一个新的计时器。
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(updateSystems) userInfo:nil repeats:NO];
当计时器触发时,应用程序会发出新的服务器请求。
How long does it take before the NSTimer auto-invalidates itself when using a timer with repeats:NO.
Could this be a false alert when running the profiler for leaking?
When I have made a server request, I get a success or a failure. Either way I create a new timer
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(updateSystems) userInfo:nil repeats:NO];
When the timer fires, the application makes a new server request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
达到您设置的时间间隔后,计时器将失效,并且不再执行循环。因此,如果您设置
repeats:NO
,它将自行失效。我认为泄漏不在
NSTimer
中,而是在它执行的选择器中,由您处理。使用 Xcode 的分析器工具 (CMD-Shift-A) 查找泄漏位置和/或检查您是否释放了alloc
&retain
在该选择器中。NSTimer
是无辜的。 :)A timer will invalidate after the time interval you set has been reached and it has no further loops to execute. So if you set
repeats:NO
, it will invalidate itself.I think that the leaks aren't in
NSTimer
, but in the selector it executes instead, which is handled by you. Use Xcode's Analyzer Tool (CMD-Shift-A) to find where the leaks are and/or check that you release whatever youalloc
&retain
in that selector.NSTimer
is innocent. :)计时器在触发后将失效(在您给定的时间间隔之后)。
请参阅 ref 。
另外,在计时器调用的选择器内部,代码块应该被 NSAutoreleasePool 覆盖。
选择器方法应该是这样的。
The timer will be invalidated after it fires (after the time interval u have given).
See ref.
Also, inside the selector that is invocated by the timer, the code block should be covered with NSAutoreleasePool.
The selector method should be like this.