I have recently found that when waiting for my NSURLConnections to come through it works much better if I tell the waiting thread to do:
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
instead of
[NSThread sleepForTimeInterval:1];
After reading a bit about NSRunLoop runMode:beforeDate: it sounds like it is preferable over sleep just about always. Have people found this to be true?
Yes, NSRunLoop is better because it allows the runloop to respond to events while you wait. If you just sleep your thread your app will block even if events arrive (like the network responses you are waiting for).
I usually have this construction:
while ([self isFinished] == NO) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
And then have isFinished return true when you want to stop blocking. Eith
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With