I am having a strange issue. My NSTimer CallMe method does not get fired without CFRunLoop. And if I add CFRunLoop then the line after CFRunLoop never gets called. I want both the line after CFRunLoop and CallMe method to get fired.
EDIT – I dont care about CFRunLoop just want the the timer to fire every 20 secs.
I have added the code and the comments below
int main(int argc, char * const argv[]) {
Updater *theUpdater = [[Updater alloc] init];
NSTimer *theTimer = [theUpdater getTimer];
[theTimer retain];
//CFRunLoopRun();
NSLog(@"I am here"); //If I uncomment CFRUnLoop this line does not get executed
}
@implementation Updater
- (NSTimer *)getTimer {
NSLog(@"Timer started");
NSTimer *theTimer;
theTimer = [NSTimer scheduledTimerWithTimeInterval:20.0
target:self
selector:@selector(CallMe:)
userInfo:nil
repeats:YES];
return theTimer;
}
-(void) CallMe:(NSTimer*)theTimer{
NSLog(@"I Never get called");
}
@end
Your timer needs to be created in the context of your CFRunLoop. So your code should look more like this:
You should have the Updater object retain your timer, since it is its owner. Note that when you invoke the run function that it will never terminate. For other options consult the NSRunLoop documentation.