This is a quick post about the usage of NSTimer to create a multi-threaded experience within an iPhone application. It does NOT address "backgrounding" or other challenges with creating threads in the highly restricted iPhone OS runtime.
I encountered this problem when I was trying to recreate a stopwatch application similar to the "Stopwatch" tab in the "Clock" application. I had an NSTimer object firing every tenth of a second, updating the label displaying the duration by calling a method "updateLabels".
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(updateLabels) userInfo:nil repeats:YES];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(updateLabels) userInfo:nil repeats:YES];
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[pool release];
No comments:
Post a Comment