Question

I might be misunderstanding what the function runMode:beforeDate: does but why does this seem to run forever (at least beyond the 10 seconds set).

NSRunLoop *theRL = [NSRunLoop currentRunLoop];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
while ([theRL runMode:NSDefaultRunLoopMode beforeDate:date]) {
    NSLog(@"in loop %d %@ %@", [theRL runMode:NSDefaultRunLoopMode beforeDate:date], date, [NSDate date]);
}
Était-ce utile?

La solution

Because you put a loop around it. (You actually call it twice per loop, since you call it again in the NSLog() call.)

-runMode:beforeDate: returns YES if the run loop has any sources or timers. Running a run loop services those sources and timers but doesn't remove or "consume" them. So, if you loop once, you're likely to loop forever. (If all input sources and timers were removed from the run loop, your while loop would exit, but you can't rely on that. The frameworks may add sources or timers that you don't control to the run loop.)

What are you trying to do? Why are you running the run loop anyway?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top