Reputation: 2192
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
Upvotes: 2
Views: 1194
Reputation: 80603
Your timer needs to be created in the context of your CFRunLoop. So your code should look more like this:
NSRunLoop* myRunLoop = [NSRunLoop currentRunLoop];
Updater * theUpdater = [[Updater alloc] init];
[myRunLoop run]
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.
Upvotes: 1
Reputation: 53551
This is the expected behavior. From the CFRunLoop
docs (emphasis added):
CFRunLoopRun
Runs the current thread’s CFRunLoop object in its default mode indefinitely.
In other words, CFRunLoopRun()
never returns, unless you call CFRunLoopStop()
somewhere.
Upvotes: 1