nstimer stop when touch the screen

I have a timer class, with a nstimer inside, who push notification to a uitableviewcontroller, and the notification object(value of the timer) is affected in a uitableviewcell. My problem is when I touch the screen to scroll, the timer stop, NSTimer don't start in a other thread? How I can fix this problem.

My timerClass

#import "timerEtape.h"
#import "FonctionUtile.h"
#import "Mission.h"

static timerEtape *sngTimerEtape = nil;

@implementation timerEtape

@synthesize repeatingTimerEtape;
@synthesize dateComp;
@synthesize questionCircuit;
@synthesize b_Pause;

+(timerEtape *) singletonTimer
{
    @synchronized(self){
        if (sngTimerEtape == nil ) 
        {
            sngTimerEtape = [[timerEtape alloc]init];
        }
    }

    return sngTimerEtape;
}

-(id)init
{
    self = [super init];

    if (self != nil) {
        dateComp = [[NSDateComponents alloc] init];
        b_Pause = FALSE;
    }

    return self;
}

- (void)startTimer {

    [repeatingTimerEtape invalidate];

    NSString * temps; 

    if (questionCircuit) {

        temps = [[Mission singletonMission].circuitTerrain valeurChampEtapeCircuitEnCours:@"et_temps_etape" :FALSE];
    }
    else {
        temps = [[Mission singletonMission].histoireTerrain valeurChampQuestion:@"hi_temps_etape" :FALSE];
    }

    if (!b_Pause) {
        [dateComp setHour:0];
        [dateComp setMinute:[[FonctionUtile gauche:temps :2] intValue]];
        [dateComp setSecond:[[FonctionUtile droite:temps :2] intValue]];
    }
    else {
        b_Pause = FALSE;
    }

    self.repeatingTimerEtape = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateLabelTimerEtape:) userInfo:nil repeats:YES];
}

-(void)pause
{
    self.b_Pause = TRUE;
    [repeatingTimerEtape invalidate];
}

-(void)updateLabelTimerEtape:(NSTimer*)theTimer
{

    if ([dateComp second] == 0) {

        if ([dateComp minute] == 0) {
            if ([dateComp hour] != 0) {
                [dateComp setHour:[dateComp hour] -1];
                [dateComp setMinute:59];
                [dateComp setSecond:59];
            }
            else {
                [repeatingTimerEtape invalidate];
                [delegate performSelector:touchAction];
            }

        }
        else {
            [dateComp setMinute:[dateComp minute] -1];
            [dateComp setSecond:59];
        }
    }
    else {
        [dateComp setSecond:[dateComp second] -1];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"rafraichirTimerEtape" object:[NSString stringWithFormat:@"%02d:%02d", [dateComp minute],[dateComp second]]];
}

-(void)setTarget:(id)target andAction:(SEL)action {
    delegate = target;
    touchAction = action;
}

-(void)dealloc
{
    [dateComp release];
    [repeatingTimerEtape release];

    [super dealloc];
}
@end

and when I receive the notification in my uitableviewcontroller class

-(void)rafraichirTimerEtape:(NSNotification*)notification
{
    [tempsRestant release];
    tempsRestant = [[notification object]copy];

    [table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]].detailTextLabel.text = [notification object];
}   

thanx

Answers


I have encountered with this problem before. In fact, NSTimer does not run in another thread, it always run in the thread which it is started. There is a runloop concept in iphone sdk, you should read it from here to understand timers. In this concept, you push some jobs into runloop and runloop implements them sequentially. The main thread is always a run loop and ui jobs are run there. So if you start your timer in the main thread, it will be affected by ui processing. You should start a new thread, configure it as a run loop and start your timers there.

Edit: Here is a sample code from the link that I posted. threadMain is the first function which the thread starts.

- (void) threadMain
{
    NSRunLoop* myRunLoop = [NSRunLoop currentRunLoop];

    // Create and schedule the timer.
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
                selector:@selector(doFireTimer:) userInfo:nil repeats:YES];

    NSInteger loopCount = 10;

    do
    {
        // Run the run loop 10 times to let the timer fire.
        [myRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
        loopCount--;
    }
    while (loopCount);
}

if you want runloop to run infinitely, but terminate it with a condition, use this instead

BOOL shouldKeepRunning = YES;        // global

NSRunLoop *theRL = [NSRunLoop currentRunLoop];

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self
      selector:@selector(doFireTimer:) userInfo:nil repeats:YES];

while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode 
      beforeDate:[NSDate distantFuture]]);

Need Your Help

Find land mass nearest to a geo coordinate

geolocation geospatial reverse-geocoding

As part of a developer challenge, I am trying to determine the land mass closest to a given coordinate. Obviously, if the point is on land, I use reverse geocoding and can get details. The proble...

Simple FTP sample - connection closing causes files to get trunctated

cocoa file-upload ftp corruption

I have taken apple's simple FTP Sample and edited it to my needs. (did not change the uploading part)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.