快速崩溃与多个异步调用

时间:2019-05-28 10:26:35

标签: swift

在以下位置执行源代码时,以下应用程序崩溃:   u.VerificaPermesso(...),应用程序在n个调用节点的其余服务器上进行检查。代码正确,但是当机了吗?您能向我解释原因吗?调用到rest的node.js服务器,该服务器以true或false值响应,并根据该值是否显示来进行

快捷代码:

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()



        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)



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

        DispatchQueue.main.async {

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

            //Controllo permesso accesso marcatempo
            u.VerificaPermesso(TipologiaPermesso: "marcatempo", completion: { result in
                DispatchQueue.main.async {
                     if(result == "false") {
                    viewControllerList.removeAll(where:{ $0 is MarcaTempoViewController })

                    self.viewControllers = viewControllerList
                    }
                }
            });

            u.VerificaPermesso(TipologiaPermesso: "cantieri", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                       viewControllerList.removeAll(where:{ $0 is CantieriViewController })

                        self.viewControllers = viewControllerList
                    }
                }
            });

            //Controllo permesso accesso Articoli
            u.VerificaPermesso(TipologiaPermesso: "articoli", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                         viewControllerList.removeAll(where:{ $0 is RicercaArticoliViewController })

                        self.viewControllers = viewControllerList
                    }
                }
            });

            //Controllo permesso accesso Clienti
            u.VerificaPermesso(TipologiaPermesso: "clienti", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                       viewControllerList.removeAll(where:{ $0 is ClienteViewController })
                          self.viewControllers = viewControllerList
                    }
                }
            });





        }

    }

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

}

1 个答案:

答案 0 :(得分:0)

我想原因是不均衡的调用在这里

viewControllerList.remove(at: 1)
viewControllerList.remove(at: 5)

表示如果删除索引1的say元素时数组计数为6,则意味着尝试删除5时数组将崩溃,因为现在的最大计数为4


您可以避免在remove e.x中使用索引而不是viewControllerList.remove(at: 0)

viewControllerList.removeAll(where:{ $0 is MarcaTempoView })