Swift 崩溃与多个异步

标签 swift

下面的代码对服务器执行一系列 rest 调用,以显示或不显示 Controller 栏选项卡内的 View Controller ,事实是当它必须显示 View Controller 时(TipologiaPermesso:“magazzino”)它进入崩溃,这是由于什么?以及我该如何解决这个问题,所有调用都是异步到以 json 响应的 node.js 服务器

代码:

import UIKit

class TabBarViewController: UITabBarController {

    var u: User = User()
    /* Genero L'UITabBarController creando le ViewController ed inserendole in un array! */
    override func viewDidLoad() {
        super.viewDidLoad()
        var tabFrame = self.tabBar.frame
        tabFrame.size.height = 60
        self.tabBar.frame = tabFrame
        hideKeyboardWhenTappedAround()



        //Controllo permesso accesso cantieri
        let u = User()



        let MarcaTempoView = MarcaTempoViewController()
        MarcaTempoView.tabBarItem = UITabBarItem(title: "Marca Tempo", image: UIImage(named: "clock.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 0)

        let CantieriView = CantieriViewController()
        CantieriView.tabBarItem = UITabBarItem(title: "Cantieri", image: UIImage(named: "home.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 1)

        let ArticoliView = RicercaArticoliViewController()
        ArticoliView.tabBarItem = UITabBarItem(title: "Articoli", image: UIImage(named: "articoli.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 2)

        let UserView = UserViewController()
        UserView.tabBarItem = UITabBarItem(title: "Utente", image: UIImage(named: "user.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 3)

        let ClienteView = ClienteViewController()
        ClienteView.tabBarItem = UITabBarItem(title: "Clienti", image: UIImage(named: "risorse_umane.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 4)

        let MagazzinoView = RicercaArticoliViewController()
        MagazzinoView.Stato = "Magazzino"
        MagazzinoView.u = u
        MagazzinoView.tabBarItem = UITabBarItem(title: "Magazzino", image: UIImage(named: "warehouse.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 5)

        var viewControllerList = [MarcaTempoView, CantieriView, ArticoliView, UserView, ClienteView, MagazzinoView]

        DispatchQueue.main.async {

            //Controllo permesso accesso marcatempo
            u.VerificaPermesso(TipologiaPermesso: "marcatempo", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                        viewControllerList.remove(at: 0)
                        self.viewControllers = viewControllerList
                    }
                }
            });

            //Controllo permesso accesso Clienti
            u.VerificaPermesso(TipologiaPermesso: "clienti", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                        viewControllerList.remove(at: 4)
                        self.viewControllers = viewControllerList
                    }
                }
            });

            //Controllo permesso accesso Articoli
            u.VerificaPermesso(TipologiaPermesso: "articoli", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                        viewControllerList.remove(at: 2)
                        self.viewControllers = viewControllerList
                    }
                }
            });

            //Controllo permesso accesso Magazzino
            u.VerificaPermesso(TipologiaPermesso: "magazzino", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                        viewControllerList.remove(at: 5)
                        self.viewControllers = viewControllerList
                    }
                }
            });

            u.VerificaPermesso(TipologiaPermesso: "cantieri", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                        viewControllerList.remove(at: 1)
                        self.viewControllers = viewControllerList
                    }
                }
            });
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

最佳答案

我认为这是在所有服务器调用之前 DispatchQueue.main.async 发生的原因

关于Swift 崩溃与多个异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55947928/

相关文章:

ios - Swift - 随 UITextView 一起增长的静态 UITableView

ios - 在 uiimage 中以正确的方式快速显示小或大的高度、宽度图像

ios - 从其他方法中删除 subview

ios - 将带有数字的标签与带有数字的文本字段相乘

swift - firebase 查询和 swift

xcode - 从 'String' 到 'String' 的条件向下转换总是成功 - Swift 错误

android - Kotlin 中的 Swift 等效数据结构

ios - 从中心变换 UIView

ios - 如何确保只显示来自 Rest api 的更新数据

ios - 如何在 Swift 中的 WkWebView 中设置 Cookie?