iphone - nstimer stop when touch the screen -


i have timer class, nstimer inside, push notification uitableviewcontroller, , notification object(value of timer) affected in uitableviewcell. problem when touch screen scroll, timer stop, nstimer don't start in other thread? how can fix 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 receive notification in uitableviewcontroller class

-(void)rafraichirtimeretape:(nsnotification*)notification {     [tempsrestant release];     tempsrestant = [[notification object]copy];      [table cellforrowatindexpath:[nsindexpath indexpathforrow:2 insection:0]].detailtextlabel.text = [notification object]; }    

thanx

i have encountered problem before. in fact, nstimer not run in thread, run in thread started. there runloop concept in iphone sdk, should read here understand timers. in concept, push jobs runloop , runloop implements them sequentially. main thread run loop , ui jobs run there. if start timer in main thread, affected ui processing. should start new thread, configure run loop , start timers there.

edit: here sample code link posted. threadmain first function thread starts.

 - (void) threadmain {     nsrunloop* myrunloop = [nsrunloop currentrunloop];      // create , schedule timer.     [nstimer scheduledtimerwithtimeinterval:0.1 target:self                 selector:@selector(dofiretimer:) userinfo:nil repeats:yes];      nsinteger loopcount = 10;          {         // run run loop 10 times let timer fire.         [myrunloop rununtildate:[nsdate datewithtimeintervalsincenow:1]];         loopcount--;     }     while (loopcount); } 

if want runloop run infinitely, terminate condition, use 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]]); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -