Reputation: 13993
I encountered CFRunLoopRun()
in a codebase. It causes a login form to load indefinitely. I am trying to divine any possible reason the original developer may have used this so that I can make a sound decision about removing it.
Upvotes: 0
Views: 1048
Reputation: 299345
It is normal to run CFRunLoopRun on any thread that you want to have a run loop. It's run by the main thread at the start of most Cocoa programs, but if you create additional threads that need their own run loops, it would be fairly normal to run it there as well. I often use this when building command-line apps that need access to Cocoa. As an example of why you'd do this, it's required if you want to use NSTimer on a background thread. (I've seen people try to do this inside of GCD queues, but that's usually a mistake. Queues are not threads.) That said, since 10.6, IMO it's generally better to use GCD rather than threads and secondary run loops. And if you do need a manually-maintained thread, it's probably for something that doesn't need a run loop.
It is possible to use CFRunLoopRun recursively on an existing run loop. It's a bit more common to use CFRunLoopRunInMode instead, but in either case it's usually a sign that someone is trying to turn an asynchronous operation into a synchronous operation by pumping the run loop until the operation completes. This is fairly dangerous and can cause very strange side-effects. I've used it before, but it's kind of like swizzling; when it bites you, the bugs are mind-bending.
Upvotes: 3