问题
I created a simple singleton and run method in it:
- (void)run {
static int times = 0;
NSLog(@"times = %d", times++);
[self performSelector:@selector(run) withObject:nil afterDelay:MIN_DELAY];
}
But it doesn't work properly. It is executed only once.
But if I replace performSelector:withObject:afterDelay:
with performSelector:
then it will be called a lot of times (but I need a delay between calls).
So why method performSelector:withObject:afterDelay:
doesn't work? And can I use this method at all?
回答1:
Calls to -performSelector:withObject:afterDelay:
require a run loop. Console applications do not, by default, pass control into the run loop ever. For more info, search for NSRunLoop.
回答2:
From the docs:
This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly.
You have no runloop. Ipso facto, this method does not perform correctly for you.
(Creating and starting a runloop is one of the things that calling UIApplicationMain
does, but of course you are never calling it.)
来源:https://stackoverflow.com/questions/21687774/mac-os-console-application-performselectorwithobjectafterdelay-doesnt-work