gpt4 book ai didi

ios - 类(class)开始时 UIViewController 不显示

转载 作者:行者123 更新时间:2023-11-30 14:07:28 25 4
gpt4 key购买 nike

我启动了一个 UIViewController 并启动了 viewDidLoad 方法。但是,直到所有其他代码执行完毕后,实际 View 才会显示。为什么?

class Mall_restore_2: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
println("started Mall_restore_2")
restore_map() // this executes for 20-30 seconds
====view appears now, i.e. after 30 seconds delay =========
}

View Controller 通过以下方式实例化:

    self.storyboard!.instantiateViewControllerWithIdentifier("Mall_restore_2_panel") as! Mall_restore_2

最佳答案

您已发现问题,它与您长时间运行的任务相关。长任务不应在主线程中运行。

以下是如何在 Swift 中执行此操作:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
restore_map()
})

这将在默认后台队列中运行restore_map函数....完成后,您可以在主线程中调用更新函数或发送消息。

例如:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
restore_map()
dispatch_async(dispatch_get_main_queue()) {
// update your UI following the restore_map function
}
})

关于ios - 类(class)开始时 UIViewController 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com