2

The code below performs a series of rest calls to a server, to display or not view controllers inside a controller bar tab, the fact is that when it has to display the view controller (TipologiaPermesso: "magazzino") it goes into crash, what is it due? and how can I solve this, all calls are asynchronous to a node.js server that responds with a json

Code:

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

}
  • 6
    show us the error log. – Enea Dume May 02 '19 at 07:56
  • @EneaDume this is Message from debugger: Terminated due to signal 9, it's show blank screen –  May 02 '19 at 07:59
  • add an exception breakpoint to see detailed log – Enea Dume May 02 '19 at 08:04
  • with the breaking point I am not checking for errors –  May 02 '19 at 08:08
  • Not sure if this is the issue but when you do `MagazzinoView.u = u` it looks to me like `u` is nil. Also don't name property or variables with single letters, use proper names like user etc to improve readability of your code – Joakim Danielson May 02 '19 at 08:09
  • @JoakimDanielson I updated the code but I still have the same error –  May 02 '19 at 08:14
  • Please carefully read this answer. https://stackoverflow.com/questions/24056205/how-to-use-background-thread-in-swift , you are sending sever call on the main thread that should be in the background. – Muhammad Shauket May 02 '19 at 08:16
  • @ShauketSheikh I tested your solution but it doesn't work –  May 02 '19 at 08:23
  • Then you are doing something wrong. Don't make things complicated, don't update UI first just send server call and check this is working properly or not. if it is working properly then go for UI update. Hope so you can get root cause. Otherwise it's waste of time for you. – Muhammad Shauket May 03 '19 at 02:03

2 Answers2

0

I think this is happening cause of your DispatchQueue.main.async before all your server calls

Alastar
  • 1,284
  • 1
  • 8
  • 14
0

try this code

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


Picker
  • 688
  • 1
  • 7
  • 7