28
May/09
8

Running NSTimer on Separate Thread

I’ve been working in something requiring an NTimer to fire every second. The documentation says that an NSTimer needs to be attached to a NSRunLoop to fire. So I started off with the following:

NSTimer* timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];

So, what I am doing here is creating a new NSTimer which fires in 1 second intervals and sends the timerTick message to self every time it fires. On the 2nd line, I am attaching the NSTimer object to the current runloop. This all worked fine. Until…!