如何在没有任何交互的情况下在 swiftUI 中 x 秒后推送视图?
NavigationView{
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now()+1.0) {
NavigationLink(destination: pageViewUi()) {
EmptyView()
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想推送一个视图控制器,并希望其余任务在后台工作而不冻结 UI。但要么我的 UI 冻结,要么后台线程之前执行。附上我工作的一个小例子。
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
}
print("This is run on the background queue")
}
Run Code Online (Sandbox Code Playgroud)
结果:
This is run on the background queue
This is run on the main queue, after the previous code in outer block
Run Code Online (Sandbox Code Playgroud)
主队列应该比后台线程先执行。