在 viewWillDisappear 上显示 SVProgressHUD

问题描述

上下文:我的 UIViewController 正在显示一个编辑个人资料视图,其中有几个 UITextField(名字、姓氏、生日……)。没有“保存按钮”,只要调用 textFieldDidEndEditing数据库上的值就会更新。

如果用户尝试在所有数据库更新完成之前离开 UIViewController,我想显示加载视图(我使用的是 SVProgressHUD 库)。

这是我的代码(如果 updateWaitingConfirm 为空,则表示所有更新都已完成):

override func viewWilldisappear(_ animated: Bool) {
    
    if !updateWaitingConfirm.isEmpty {
        
        self.showSVProgressHUD(message: NSLocalizedString("updateWaiting",comment: ""))
        
        timetoWaitCount = 0
        let group = dispatchGroup()
        group.enter()
        
        checkWaitingConfirmEmpty(group)
        
        group.wait()
        self.dismissSVProgressHUD()
    }
    
    super.viewWilldisappear(animated)
}

func checkWaitingConfirmEmpty(_ group: dispatchGroup) {
    
    dispatchQueue.global(qos: .default).asyncAfter(deadline: .Now() + 0.5) {
        self.timetoWaitCount += 0.5
        if self.updateWaitingConfirm.isEmpty || self.timetoWaitCount > self.maxTimetoWaitForUpdates {
            group.leave()
        } else {
            self.checkWaitingConfirmEmpty(group)
        }
    }
}

当所有更新完成后(或超过 maxTimetoWaitForUpdates(10 秒)),group.leave()调用,然后我调用我的函数关闭 HUD 并调用 super.viewWilldisappear()

问题是我的视图中没有显示任何内容,例如我还尝试编辑文本字段,但它也不起作用。 我还尝试在 showSVProgressHUD 之后调用 view.layoutIfNeeded(),但它不起作用。

很高兴知道:我不能只在我的后退按钮 touchUpInside 方法调用一个方法,因为用户也可以使用 UIViewController 退出 interactivePopGestureRecognizer

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)