を追加することができますいずれか
ちょうどタイマーを作成する実行を開始しません。作成してスケジュールする必要があります。
バックグラウンドスレッドで実行したい場合は、実際にはそれよりも少し多くの作業を行う必要があります。 NSTimer
は、NSRunloop
に添付されています。これは、イベントループのCocoa形式です。それぞれNSThread
には本質的に実行ループがありますが、明示的に実行するように指示する必要があります。
タイマー付きの実行ループは無期限に実行できますが、自動解放プールを管理していないため、実行したくない可能性があります。
要するに、(i)タイマーを作成したいと思うかもしれません。 (ii)そのスレッドの実行ループにアタッチします。 (iii)自動解放プールを作成するループを入力し、実行ループを少し実行した後、自動解放プールを廃棄します。
// create timer
timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(timerRun)
userInfo:nil
repeats:YES];
// attach the timer to this thread's run loop
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
// pump the run loop until someone tells us to stop
while(!someQuitCondition)
{
// create a autorelease pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// allow the run loop to run for, arbitrarily, 2 seconds
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
// drain the pool
[pool drain];
}
// clean up after the timer
[timer invalidate];
'@selector(createTimmer)'おそらく単なるタイプミスです:
コードは、おそらくのように見えるのだろうか? – Tommy
なぜタイマーを作成するために別のスレッドを使用しているのか不思議です...何らかの理由で長時間動作することが予想されますか? – devios1