Sleep和NSRunLoop runMode:beforeDate之间的区别: [英] Difference between Sleep and NSRunLoop runMode:beforeDate:

查看:152
本文介绍了Sleep和NSRunLoop runMode:beforeDate之间的区别:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现,当我告诉等待线程这样做时,在等待我的NSURLConnections通过时,效果会更好:

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]];

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

代替

[NSThread sleepForTimeInterval:1];

[NSThread sleepForTimeInterval:1];

在阅读了有关NSRunLoop runMode:beforeDate的知识之后:听起来似乎总是比睡眠更可取.人们发现这是真的吗?

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?

推荐答案

是的,NSRunLoop更好,因为它允许runloop在您等待时响应事件.如果您只是休眠线程,则即使事件到来(例如您正在等待的网络响应),您的应用也会被阻止.

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).

我通常有这样的构造:

while ([self isFinished] == NO) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

然后,当您要停止阻止时,让isFinished返回true.要么

And then have isFinished return true when you want to stop blocking. Eith

这篇关于Sleep和NSRunLoop runMode:beforeDate之间的区别:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆