Ios 关闭模式视图控制器偶尔会冻结应用程序,swift 3
在我的应用程序中,我有一个视图控制器,我以模态的方式呈现。在这个视图控制器中,我有一个表视图。每当用户在表视图中进行选择时,我都会关闭视图控制器 问题是,有时视图控制器没有被解除,或者在长时间延迟(5-7秒)后被解除,即使调用了解除功能 这是我的密码:Ios 关闭模式视图控制器偶尔会冻结应用程序,swift 3,ios,swift3,modalviewcontroller,Ios,Swift3,Modalviewcontroller,在我的应用程序中,我有一个视图控制器,我以模态的方式呈现。在这个视图控制器中,我有一个表视图。每当用户在表视图中进行选择时,我都会关闭视图控制器 问题是,有时视图控制器没有被解除,或者在长时间延迟(5-7秒)后被解除,即使调用了解除功能 这是我的密码: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView == self.quarterTableView
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")
}
}
这样做会造成任何伤害吗?尝试使用分派
分派队列
DispatchQueue.main.async(execute: {
})
如果希望UI上立即发生某些事情,请在主队列上执行它
DispatchQueue.main.async(execute: {
self.dismiss(animated: true) {
Print("finished")
})
没有害处。您只需让主线程同时执行两个任务。这是一个有点旧的线程,但它可能会帮助其他线程 我最近也遇到了同样的问题(使用XCode 10.1、Swift 4.2),并且发现,是的,上述在DispatchQueue.main.async中包装解雇的解决方案是有效的。但是,这没有任何意义,因为执行dismise的线程只能是主线程 我的情况稍有不同,因为我调用了一个代理(该代理是首先显示模态视图的VC),但其行为与OP的描述相同-如果我使用“取消”按钮取消模态,对关闭完成块的响应是瞬时的,而如果通过在tableView中选择一行关闭,则在执行关闭完成块之前会有7-10秒的延迟。在发出DISCLISE之前和在完成回调中测试哪个线程正在运行,这都表明它是主线程(实际上,它不可能是其他任何线程)。因此,虽然在DispatchQueue.main.async中包装disclose显然是可行的,但它没有任何意义 请参阅下面的代码段,添加一行以取消选择表视图中的行也可以解决延迟问题。奇怪
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")
})
}
}
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
debug(“在tableView委托中:didSelectRowAt”)
selectedItem=discoveredPeripherals[indexPath.row][kPeripheralUuidKey]?无
//根据所选行获取所需的数据,然后取消选择该行
//修复了延迟问题
tableView.decelerow(at:indepath,animated:false)//Eugene,您不应该在分派主块中调用它,除非您是从后台队列调用解雇(出于未知原因)尝试在原始代码中打印Thread.isMainThread,看看它说什么我也有同样的问题。你对此有任何更新或替代解决方案吗?恐怕没有新的更新。行tableView.Declerow…在退出之前取消选择任何行修复了问题,但我不知道为什么…因为我一直在寻找它更深刻的理解。。。