Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MainViewController swift上执行任务后解除警报_Swift_Alert_Loading_Dismiss - Fatal编程技术网

在MainViewController swift上执行任务后解除警报

在MainViewController swift上执行任务后解除警报,swift,alert,loading,dismiss,Swift,Alert,Loading,Dismiss,当用户第一次登录我的应用程序时,我需要从API下载一些内容,并向他们展示我正在这么做。我在MainViewController中执行此操作: override func viewDidAppear(_ animated: Bool) { let alert = UIAlertController(title: nil, message: "Wait please...", preferredStyle: .alert) let loadingIndicator

当用户第一次登录我的应用程序时,我需要从API下载一些内容,并向他们展示我正在这么做。我在MainViewController中执行此操作:

override func viewDidAppear(_ animated: Bool) {
        let alert = UIAlertController(title: nil, message: "Wait please...", preferredStyle: .alert)

        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.style = UIActivityIndicatorView.Style.gray
        loadingIndicator.startAnimating();

        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)

        let parceiroId = self.defaults.getParceiroId()
        if !self.defaults.getDownloadInicialTipoEntrega() {
            TipoEntregaAPI().loadTiposEntrega(parceiroId){ (dados) in
                if dados != nil {
                    for tipoEntrega in dados!{
                        // Do some stuff, no errors
                    }
                }
            }
        }

        if !self.defaults.getDownloadInicialPedido() {
            PedidoAPI().loadOrders(parceiroId){ (dados) in
                if dados != nil {
                    for pedidos in dados!{
                        // Do some stuff, no errors
                    }
                }
            }
        }
        self.dismiss(animated: false, completion: { () in print("Done") })
    }
问题是我的装载警报从未被解除。它从不打印“完成”。有人能帮我吗

我不知道它是否有用,但我总是收到这样的警告:

Warning: Attempt to dismiss from view controller <MyApp.MainViewController: 0x0000000> while a presentation or dismiss is in progress!
警告:在演示或解除过程中,尝试从视图控制器解除!

问题正是错误所说的。您正在呼叫
self。您的演示文稿呼叫尚未完成。请退出(…)
。为了进一步解释,您正在使用
animated:true
参数调用
present(警报,动画:true,完成:nil)
,因此它不会立即完成演示。另一方面,您在相对较短的指令块中的同一线程上调用
self.dismise(动画:false,完成:{()in print(“Done”)})
,因此它会在iOS完成对话框的动画演示之前执行,这就是为什么会出现错误的原因

但是,在实际解决问题之前,你应该问问自己,你真的想在对话框一出现就立即关闭它吗。根据您发布的代码判断,我假设您希望在其中一个或两个API调用完成后将其删除。如果是这种情况,您需要在API调用的完成块(闭包)内移动
discouse
方法调用。

我的解决方案(工作正常,打印“完成”):


当您将调用
discouse
移动到完成块中时,让您在主队列中调用它。我试图找到此警告的解决方案,但这是目标C中的所有内容,我能理解的内容与我的问题不同。直到我试图结束我的任务,我才能修复它。我在我的帖子中添加了一条关于解决方案的评论。感谢上面对我的解答。
override func viewDidAppear(_ animated: Bool) {
        if !self.defaults.getDownloadInicialTipoEntrega() || !self.defaults.getDownloadInicialPedido() || !self.defaults.getDownloadInicialVitrine() {
            Functions.showAlertWaiting("Wait please...", self)
        }

        loadDeliveryTypesNOrders { (completed) in
            if completed {
                self.dismiss(animated: false, completion: { () in print("Done") })
            }
        }
    }

func loadDeliveryTypesNOrders (completion: @escaping (Bool) -> ()) {
        let parceiroId = self.defaults.getParceiroId()
        if !self.defaults.getDownloadInicialTipoEntrega() {
            TipoEntregaAPI().loadTiposEntrega(parceiroId){ (dados) in
                if dados != nil {
                    for tipoEntrega in dados!{
                        // Do some stuff, no errors
                    }
                }
            }
        }

        if !self.defaults.getDownloadInicialPedido() {
            PedidoAPI().loadOrders(parceiroId){ (dados) in
                if dados != nil {
                    for pedidos in dados!{
                        // Do some stuff, no errors
                    }
                }
            }
        }
        completion(true)
}