The easiest way is to schedule NSTimer in the main thread of the loop. I suggest that the following code be implemented on application deletion, and you call setupTimer from applicationDidFinishLaunching:
-(void)setupTimer; { NSTimer* timer = [NSTimer timerWithTimeInterval:10 * 60 target:self selector:@selector(triggerTimer:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } -(void)triggerTimer:(NSTimer*)timer; {
If your material here takes a lot of time, and you cannot delay the main thread, then either call your things using:
[self performSelectorInBackground:@selector(myStuff) withObject:nil];
Or you can run NSTimer in the background thread using something like this (I intentionally leak the stream object):
-(void)startTimerThread; { NSThread* thread = [[NSThread alloc] initWithTarget:self selector:@selector(setupTimerThread) withObject:nil]; [thread start]; } -(void)setupTimerThread; { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSTimer* timer = [NSTimer timerWithTimeInterval:10 * 60 target:self selector:@selector(triggerTimer:) userInfo:nil repeats:YES]; NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; [runLoop addTimer:timer forModes:NSRunLoopCommonModes]; [runLoop run]; [pool release]; } -(void)triggerTimer:(NSTimer*)timer; {
source share