Objective-C类时间延迟?
我想知道是否有人知道 Objective-C 中的一个类可以在运行时的指定时间停止/延迟应用程序? 我已经查看了时间管理器和 NSTimer,但不知道如何在其中添加延迟。 我以前从未使用过这两个类,所以也许其他人已经使用过并且可以给我一些提示? 谢谢!
I was wondering if anyone knows of a class in objective-C that stops/delays the application at a specified amount of time during runtime? I've already looked at the Time Manager and NSTimer and don't see how I could possibly put a delay in there. I've never used these two classes before though, so maybe someone else has and could give me a tip? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上取决于您所说的“停止/延迟应用程序”的含义。 如果您有一个单线程应用程序,您可以使用各种睡眠调用中的任何一个(usleep、sleep、nanosleep 等)。 但这可能不是您想要的,因为它只会导致用户的 UI 挂起,并且不会停止任何后台线程。
要真正给出一个像样的答案,可能有必要知道您实际上想要做什么。 您是否在等待某些事情,轮询资源等?
It really depends what you mean by "stops/delays the app." If you have a single threaded app you can just use anyone of the various sleep calls (usleep, sleep, nanosleep, etc). That is probably not what you want though, because it will just result in a hung UI for the user, and won't stop any background threads.
To actually give a decent answer it is probably necessary to know what you are actually trying to do. Are you waiting for something, polling a resource, etc?
我认为您正在寻找 [NSThread sleepForTimeInterval:] 这应该可以解决问题。 您还应该考虑使用计时器在延迟后调用另一个函数,因为该解决方案是非阻塞的。 如果您从主线程调用 NSThread 方法,它将在时间间隔内完全锁定您的应用程序。
I think you're looking for [NSThread sleepForTimeInterval:] That should do the trick. You should also consider using a timer to invoke another function after a delay, because that solution is non-blocking. The NSThread approach will totally lock your application for the time interval if you call it from the main thread.