Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Depends on what exactly you are trying to do. Most of the time you should not mess with the run loop and simply set up a timer:</p> <pre><code>const float framerate = 40; const float frequency = 1.0f/framerate; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:frequency target:self selector:@selector(doSomething) userInfo:nil repeats:YES]; </code></pre> <p>Now the <code>doSomething</code> method is going to get executed about 40 times per second. If you want to execute something as often as possible, you can spawn a new thread:</p> <pre><code>- (void) loop { while (running) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Do something useful. [pool release]; } } - (void) run { run = YES; [NSThread detachNewThreadSelector:@selector(loop) toTarget:self withObject:nil]; } </code></pre> <p>Of course now you have threads, which means synchronizations and more headache than you would have with timers. As I said, it depends on what you want to do.</p> <p>And to answer your original question: Yes, Cocoa applications do have a “main loop” that’s probably similar to the GTK one. As the loop is already created for you, there is no point in writing another one – unless you are trying to figure out how things work or do something sophisticated. See <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW1" rel="noreferrer">Run Loop Management</a> in Threading Programming Guide for details.</p> <p>If you want to simply handle events, the default run loop will do it for you. Just implement the handlers, ie. methods connected to buttons and such stuff. If you want to do something periodically (like update an animation or so), set up a timer (<code>NSTimer</code>). The default run loop will take care of the time and it will call the appropriate selector as often as you want to.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload