Sleep 和 NSRunLoop runMode:beforeDate 之间的区别:
我最近发现,当等待 NSURLConnections 通过时,如果我告诉等待线程执行以下操作,效果会更好:
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distanceFuture]];
而不是
[NSThread sleepForTimeInterval:1];
在阅读了一些关于 NSRunLoop runMode:beforeDate: 的内容之后,听起来它总是比睡眠更好。人们发现这是真的吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,NSRunLoop 更好,因为它允许 runloop 在您等待时响应事件。如果您只是休眠线程,即使事件到达(例如您正在等待的网络响应),您的应用程序也会阻塞。
我通常有这样的结构:
然后当你想停止阻塞时让 isFinished 返回 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:
And then have isFinished return true when you want to stop blocking. Eith