快速崩溃与多个异步

时间:2019-05-02 07:54:31

标签: swift

下面的代码对服务器执行一系列的rest调用,以显示或不显示控制器栏选项卡中的视图控制器,事实是,当它必须显示视图控制器(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()
    }

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试此代码

 DispatchQueue.global(qos: .userInitiated).async {

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