Mac OS, console application. performSelector:withObject:afterDelay: doesn't work?

时光毁灭记忆、已成空白 提交于 2019-12-02 17:59:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!