Running the Run Loop Until a Predicate Succeeds
By observing
kCFRunLoopBeforeWaiting
, we can test for completion on every loop of the RunLoop. Before sleeping (i.e. waiting for an event), the RunLoop has called everything there was to call. That’s the right time to test for completion. This variant also solves the “active polling” scenarios: if thepolling_
flag is set, the RunLoop actually never sleeps and run continuously;fulfilled_
is checked on every pass. And of course, contrary to most implementations, including my own, there’s no “minimal delay”, and no additional code to handle the loop or the timeout. That should do the trick.
I’ve seen lots of implementations of this idea. I’m not sure what the Right Way is, but this seems like a good one. It uses CFRunLoopObserver
.