当视图消失时如何暂停和重新启动 NSTimer?
我在视图类中使用 NSTimer,每 15 秒调用一次。我的问题是,它工作正常,但我的应用程序变得很慢,因为它向整个应用程序显示其性能。所以我想当我的视图从其超级视图中消失时暂停 NSTimer 并在它出现时重新启动计时器。请帮助我解决问题。这是我的代码:
- (void) viewWillAppear:(BOOL)animated
{
if(!Thread_bool)
{
//[spinner startAnimating];
NSTimer *timer_new1=[[NSTimer alloc] init];
timer_new1=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(threadMethod) userInfo:nil repeats:YES];
self.timer_new=timer_new1;
[timer_new1 release];
[self.tableView setEditing:NO];
isEditing=NO;
Thread_bool=YES;
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.timer_new invalidate];
timer_new=nil;
}
I am using an NSTimer
in my view class that is called every 15 seconds. My problem is, it is working properly, but my app is getting slow, because it is showing its performance to the whole app. So I want to pause the NSTimer
when my view disappears from its superview and restart the timer when it appears. Please help me in solving the problem. Here is my code:
- (void) viewWillAppear:(BOOL)animated
{
if(!Thread_bool)
{
//[spinner startAnimating];
NSTimer *timer_new1=[[NSTimer alloc] init];
timer_new1=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(threadMethod) userInfo:nil repeats:YES];
self.timer_new=timer_new1;
[timer_new1 release];
[self.tableView setEditing:NO];
isEditing=NO;
Thread_bool=YES;
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.timer_new invalidate];
timer_new=nil;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一行中,您分配了一个计时器,在第二行中,创建了另一个计时器并将其分配给
timer_new1
。因此,您丢失了对上一行中分配的计时器的引用,并且该引用已泄漏。您不需要第一行分配。执行此操作:并删除
[timer_new1 release];
(我假设保留self.timer_new
)。同样在viewWillDisappear
中执行self.timer_new = nil;
而不是timer_new=nil;
。添加self
将调用 setter 并正确释放前一个计时器。In first line you have alloced a timer and in second line another timer is created and assigned to
timer_new1
. So you lost the reference to the timer that was alloced in previous line and that is leaked. You don't need the first line alloc. Do this:And remove
[timer_new1 release];
(I am assuming thatself.timer_new
is retained). Also inviewWillDisappear
doself.timer_new = nil;
instead oftimer_new=nil;
. Adding thatself
will call the setter and correctly release the previous timer.您有两行代码正在创建
NSTimer
的实例。第一个是,第二个是
第一行创建的 NSTimer 对象被保留,您似乎知道这一点,因为您稍后尝试释放它。然而,上面的第二行正在创建一个 NSTimer 的新实例,它是自动释放的。当您执行此操作时,第一个未释放的 NSTimer 对象将被泄漏!
如果您已设置
timer_new
的访问器方法来保留计时器,则删除上面的第一行并不释放timer_new1
。You have two lines there which are creating an instance of
NSTimer
. The first isand the second is
The
NSTimer
object created by the first line is retained, which you seem to be aware of because you're trying to release it later. However, the second line above is creating a new instance of NSTimer, which is autoreleased. When you do this, the first non-releasedNSTimer
object is being leaked!If you've set up your accessor method for
timer_new
to retain the timer, then delete the first line from above and don't releasetimer_new1
.