我想在某个点暂停我的应用程序。换句话说,我希望我的应用程序执行代码,但在某一点上,暂停4秒,然后继续执行其余的代码。我该怎么做呢?
我用的是Swift。
我想在某个点暂停我的应用程序。换句话说,我希望我的应用程序执行代码,但在某一点上,暂停4秒,然后继续执行其余的代码。我该怎么做呢?
我用的是Swift。
当前回答
我同意Palle的观点,在这里使用dispatch_after是一个很好的选择。但是您可能不喜欢GCD调用,因为编写它们非常烦人。相反,你可以添加这个方便的助手:
public func delay(bySeconds seconds: Double, dispatchLevel: DispatchLevel = .main, closure: @escaping () -> Void) {
let dispatchTime = DispatchTime.now() + seconds
dispatchLevel.dispatchQueue.asyncAfter(deadline: dispatchTime, execute: closure)
}
public enum DispatchLevel {
case main, userInteractive, userInitiated, utility, background
var dispatchQueue: DispatchQueue {
switch self {
case .main: return DispatchQueue.main
case .userInteractive: return DispatchQueue.global(qos: .userInteractive)
case .userInitiated: return DispatchQueue.global(qos: .userInitiated)
case .utility: return DispatchQueue.global(qos: .utility)
case .background: return DispatchQueue.global(qos: .background)
}
}
}
现在你只需在后台线程上延迟你的代码,就像这样:
delay(bySeconds: 1.5, dispatchLevel: .background) {
// delayed code that will run on background thread
}
在主线程上延迟代码甚至更简单:
delay(bySeconds: 1.5) {
// delayed code, by default run in main thread
}
如果你喜欢一个框架,也有一些更方便的功能,然后签出handysswift。你可以通过SwiftPM将它添加到你的项目中,然后像上面的例子一样使用它:
import HandySwift
delay(by: .seconds(1.5)) {
// delayed code
}
其他回答
你可以创建扩展来轻松使用延迟函数(语法:Swift 4.2+)
extension UIViewController {
func delay(_ delay:Double, closure:@escaping ()->()) {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}
}
如何在UIViewController中使用
self.delay(0.1, closure: {
//execute code
})
如果从UI线程调用,sleep会锁住你的程序,可以考虑使用NSTimer或分派计时器。
但是,如果你真的需要延迟当前线程:
do {
sleep(4)
}
这使用了UNIX中的睡眠函数。
要创建一个简单的时间延迟,您可以导入Darwin,然后使用sleep(秒)来执行延迟。不过,这只需要几秒钟的时间,所以为了更精确的测量,你可以导入Darwin并使用usleep(百万分之一秒)进行非常精确的测量。为了验证这一点,我写道:
import Darwin
print("This is one.")
sleep(1)
print("This is two.")
usleep(400000)
print("This is three.")
打印,然后等待1秒打印,然后等待0.4秒打印。一切都如预期般顺利。
斯威夫特5 <
使用任务。Sleep不会阻塞当前任务以外的任何代码,而且非常简单。
//Delay task by 4 seconds:
Task {
try await Task.sleep(nanoseconds: 4000000000)
//Execute your code here
}
Using a dispatch_after block is in most cases better than using sleep(time) as the thread on which the sleep is performed is blocked from doing other work. when using dispatch_after the thread which is worked on does not get blocked so it can do other work in the meantime. If you are working on the main thread of your application, using sleep(time) is bad for the user experience of your app as the UI is unresponsive during that time. Dispatch after schedules the execution of a block of code instead of freezing the thread:
斯威夫特≥ 3.0
let seconds = 4.0
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
// Put your code which should be executed with a delay here
}
Swift≥5.5(异步):
func foo() async {
try await Task.sleep(nanoseconds: UInt64(seconds * Double(NSEC_PER_SEC)))
// Put your code which should be executed with a delay here
}
Swift < 3.0
let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 4 * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {
// Put your code which should be executed with a delay here
}