2

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;  
}
jscs
  • 63,694
  • 13
  • 151
  • 195
girish
  • 900
  • 12
  • 23
  • possible duplicate of [Pausing an NSTimer](http://stackoverflow.com/questions/5862743/pausing-an-nstimer) – jscs May 18 '11 at 18:49

2 Answers2

8
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.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • hi taskinoor its not working can u please give any suggestion on this..in fact i have return this code in viewWillAppear.if i tab another tab and if i return to same tab the timer is not working..please provide me any solution. – girish May 18 '11 at 14:03
  • Add Thread_bool=NO in ViewWillDisappear or remove the if from viewWillAppear unless you are not setting Thread_bool=NO in anywhere else. – taskinoor May 18 '11 at 14:08
2

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.

CharlieMezak
  • 5,999
  • 1
  • 38
  • 54
  • hi charlie its not working can u please give any suggestion on this..in fact i have return this code in viewWillAppear.if i tab another tab and if i return to same tab the timer is not working..please provide me any solution – girish May 18 '11 at 13:59
  • Well, when you first create the timer, you're setting Thread_bool to YES. So, unless you're setting it to NO somewhere else, the timer-setup code in viewWillAppear isn't going to run when you return from the other tab. Try removing the if() from viewWillAppear to see if it works then. – CharlieMezak May 18 '11 at 14:02