
基本概念

-(void)runTimerInThread{ //NSAutoreleasePool,沒的用 [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSth:) userInfo:self repeats:YES]; //1.以上方法自動把NSTimer添加到RunLoop里面 NSTimer* timer=[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(doSth:) userInfo:self repeats:YES]; //2.以上方法不會自動NSTimer添加到RunLoop里面 [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode]; //要手動加入runLoop,額額,類方法就寫入了 addTimer:timer forMode:NSDefaultRunLoopMode; [[NSRunLoop currentRunLoop]run]; //這很重要,把定時器的線程加入運行回環runloop才會使線程處于活躍狀態}
定時器要加入runLoop才可以正常運行,但是要保證定時器的精確度,必須在不堵塞的多線程下進行
[self performSelectorInBackground:@selector(runTimerInThread) withObject:nil];
當然,你可以用CGD技術來處理多線程任務的
新聞熱點
疑難解答