【问题标题】:Dismiss modal view controller freezes the app once in a while, swift 3关闭模式视图控制器偶尔冻结应用程序,swift 3
【发布时间】:2017-01-18 17:42:04
【问题描述】:

在我的应用程序中,我有一个视图控制器,我以模态方式呈现。在这个视图控制器中,我有一个表格视图。每当用户在表格视图中进行选择时,我都会关闭视图控制器。

问题是有时即使调用了解除函数,视图控制器也没有被解除或在长时间延迟(5-7 秒)后被解除。

这是我的代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if tableView == self.quarterTableView
    {
        self.delegate?.modalViewController(modalVC: self, dismissedWithValue:self.quarterPeriods[indexPath.row])
    }
    else if tableView == self.monthTableView
    {
        self.delegate?.modalViewController(modalVC: self, dismissedWithValue: self.monthPeriods[indexPath.row])
    }

    Print("didSelectRowAt dismiss")

    self.dismiss(animated: true) { 
        Print("finished")
    }
}

非常感谢任何帮助。

编辑:

我通过以下方式解决了这个问题:

DispatchQueue.main.async
{
    self.dismiss(animated: true) {
       DDLogDebug("finished")
    }
}

这样做有什么危害吗?

【问题讨论】:

  • Eugene,你不应该在调度主块中调用它,除非你从后台队列调用解除(出于某种原因未知)尝试在原始代码中打印 Thread.isMainThread 和看看它说什么

标签: ios swift3 modalviewcontroller


【解决方案1】:

如果您希望 UI 上的某些内容立即发生,请在主队列中执行它

DispatchQueue.main.async(execute: {
    self.dismiss(animated: true) { 
    Print("finished")
})

【讨论】:

    【解决方案2】:

    尝试使用调度DispatchQueue

    DispatchQueue.main.async(execute: {
    
    })
    

    【讨论】:

      【解决方案3】:

      没有伤害。您只需让主线程同时执行两个任务即可。

      【讨论】:

        【解决方案4】:

        这是一个有点旧的线程,但它可能会帮助其他人。

        我最近遇到了同样的问题(使用 XCode 10.1、Swift 4.2)并发现,是的,上述将驳回包装在 DispatchQueue.main.async 中的解决方案有效。但是,这没有任何意义,因为执行解除的线程只能是主线程。

        我的情况略有不同,因为我回调了一个委托(委托是首先呈现模态视图的 VC),但行为与 OP 的描述相同 - 如果我用 Cancel 关闭模态按钮,对关闭完成块的响应是即时的,而如果我通过在 tableView 中选择一行来关闭,那么在执行关闭完成块之前会有 7-10 秒的延迟。在发出关闭之前和在完成回调中测试正在运行的线程都表明它是主线程(实际上,它不能是其他任何东西......)。因此,虽然在 DispatchQueue.main.async 中包装dismiss 显然有效,但它没有任何意义。

        参见下面的代码 sn-p,在表格视图中添加取消选择行的行也修复了延迟问题。很奇怪。

            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            Logger.debug("In tableView delegate: didSelectRowAt")
            selectedItem = discoveredPeripherals[indexPath.row][kPeripheralUuidKey] ?? nil
            // Grabbing the data I need based on the selected row and then deselecting the row
            // fixes the delay problem
            tableView.deselectRow(at: indexPath, animated: false) // <<---- fixes the delay
            discoveredPeripherals = []
            if let dd = dismissalDelegate { dd.didCompletePresenting(viewController: self) }
            else { self.dismiss(animated: true, completion: {
                Logger.debug("Warning: Invalid (nil) dismissal delegate, dismissing self")
                })
            }
        }
        

        希望对某人有所帮助...我从 SO 上的其他帖子中注意到,在基于 tableView 行选择呈现 VC 时存在正交问题...。

        【讨论】:

        • 我遇到了同样的问题。您对此有任何更新或替代解决方案吗?
        • 恐怕没有新的更新。行 tableView.deselectRow... 在退出之前取消选择任何行修复了问题,但我不知道为什么...自从我开始工作以来,我一直没有寻求更深入的理解...
        猜你喜欢
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-24
        • 2021-07-27
        • 1970-01-01
        相关资源
        最近更新 更多