NSTimer Lag help

Discussion in 'iOS Programming' started by uaecasher, Dec 18, 2009.

  1. uaecasher macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #1
    hello,

    I made an NSTimer to start when a user taps, my problem is that if the user taps again (after the NSTimer started) another timer will be lunched and the time displayed will be moving crazy fast, how to fix that?

    Thanks

    Code:
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    	UITouch *touch = [touches anyObject]; 
    		if(timerBOOL==TRUE)
    	{
    		[timer invalidate];
    		timerBOOL = FALSE;
    	} else { 
    		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
    				 timerBOOL = TRUE;
    				 }
    
    
    }
    
    -(void)countUp {
    	mainint += 1;
    	myTextFeild.text = [NSString stringWithFormat:@"%i secs", mainint];
    	
    }
    	
    
    
    
    
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    Check whether the timer has already begun before starting another?
     
  3. uaecasher thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #3
    sorry, my problem is that when I did the check above:

    Code:
    
    	if(timerBOOL==TRUE)
    	{
    		[timer invalidate];
    		timerBOOL = FALSE;
    	} else { 
    		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
    				 timerBOOL = TRUE;
    				 }
    
    
    the timer would lag for about 4 seconds then continue each time a user taps
     
  4. NickFalk macrumors 6502

    NickFalk

    Joined:
    Jun 9, 2004
    #4
    What are you actually trying to achieve when the player taps a second time?
     
  5. uaecasher thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #5
    I'm want to show different images depending on the number of taps (I figured how to do that, the only problem is the timer lags
     
  6. NickFalk macrumors 6502

    NickFalk

    Joined:
    Jun 9, 2004
    #6
    OK, I'm struggling to understand how this fits with running the countUp method at each tap?

    I'm also curious regarding where you actually define the timer, and why this is not done inside the touchesBegan method?
     
  7. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #7
    Try this:
    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    	UITouch *touch = [touches anyObject]; 
    		if(timerBOOL==TRUE)
    	{
    		[timer invalidate];
    		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
    	} else { 
    		timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
    				 timerBOOL = TRUE;
    				 }
    
    
    }
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    If you're just going to set timer the same way in both the if and else, in this case, it can be moved outside that logic.

    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
        UITouch *touch = [touches anyObject]; 
            if(timerBOOL==TRUE)
            {
                [timer invalidate];
            } else { 
                 timerBOOL = TRUE;
            }
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
    }
    Also, uaecasher, realize that normally Objective-C BOOL values are expressed as YES/NO, not TRUE/FALSE (although that still seems to work).

    And how does the NSTimer solve that problem? Wouldn't you rather use
    Code:
    [touches count]
    instead?
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    If you're going to post efficiency suggestions you might as well go all the way

    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
        UITouch *touch = [touches anyObject]; 
        [timer invalidate];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
    }
    
    If you invalidate the timer anywhere else set the timer ivar to nil. The timer ivar is its own BOOL.

    The usual rule for managing timers is that a timer ivar always has one of two values: a valid timer, or nil. If the timer is invalidated then set the timer ivar to nil.

    OP, the reason for the lag is that you didn't restart the timer in the case where one already existed.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    True dat.
     
  11. uaecasher thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #11
    I tired your code and same problems occurs.
    Code:
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    	UITouch *touch = [touches anyObject];
    	[timer invalidate];
    	NSUInteger tapCount = [touch tapCount];
    	taps = tapCount;
    	timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
    	
    	
    }
    
    
    @Dejo: I'm using tapcount already, the reason I'm using NSTimer as well is that I also what to know how much time did it take the user to get to the correct answer (depending on number of taps), in other words I want to control how long does it take the user to answer.

    Thanks
     
  12. NickFalk macrumors 6502

    NickFalk

    Joined:
    Jun 9, 2004
    #12
    Have a look at NSDate and NSTimeInterval.
     
  13. uaecasher thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #13
    Ok, I'm reading the documentation right now, is there anyway to stick with NSTimer as I can see that NSDate is only supported in 3.1+ and i want to support at least 3.0.0+

    Thanks
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    I'm not sure where you're seeing that but NSDate has been supported since at least 2.0.
     
  15. uaecasher thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #15
    oh sorry, in my xCode it showed the file path iPhone OS library 3.1 so I though this is the min
     

Share This Page