当视图消失时如何暂停和重新启动 NSTimer?

发布于 2024-11-08 01:31:59 字数 731 浏览 0 评论 0原文

我在视图类中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-11-15 01:31:59
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];

在第一行中,您分配了一个计时器,在第二行中,创建了另一个计时器并将其分配给 timer_new1。因此,您丢失了对上一行中分配的计时器的引用,并且该引用已泄漏。您不需要第一行分配。执行此操作:

NSTimer *timer_new1 = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(threadMethod) userInfo:nil repeats:YES];

并删除 [timer_new1 release]; (我假设保留 self.timer_new)。同样在 viewWillDisappear 中执行 self.timer_new = nil; 而不是 timer_new=nil;。添加 self 将调用 setter 并正确释放前一个计时器。

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];

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:

NSTimer *timer_new1 = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(threadMethod) userInfo:nil repeats:YES];

And remove [timer_new1 release]; (I am assuming that self.timer_new is retained). Also in viewWillDisappear do self.timer_new = nil; instead of timer_new=nil;. Adding that self will call the setter and correctly release the previous timer.

世界如花海般美丽 2024-11-15 01:31:59

您有两行代码正在创建 NSTimer 的实例。第一个是

    NSTimer *timer_new1 = [[NSTimer alloc] init];

,第二个是

    timer_new1 = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(threadMethod) userInfo:nil repeats:YES];

第一行创建的 NSTimer 对象被保留,您似乎知道这一点,因为您稍后尝试释放它。然而,上面的第二行正在创建一个 NSTimer 的实例,它是自动释放的。当您执行此操作时,第一个未释放的 NSTimer 对象将被泄漏!

如果您已设置 timer_new 的访问器方法来保留计时器,则删除上面的第一行并释放 timer_new1

You have two lines there which are creating an instance of NSTimer. The first is

    NSTimer *timer_new1 = [[NSTimer alloc] init];

and the second is

    timer_new1 = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(threadMethod) userInfo:nil repeats:YES];

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-released NSTimer 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 release timer_new1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文