Swift Switft 5在异步调用时生成异常

Swift Switft 5在异步调用时生成异常,swift,asynchronous,Swift,Asynchronous,您好,自从我将我的应用程序转换为swift 5以来,我在异步调用上出现了一系列错误,这是下面编写的代码中的一个问题,我如何解决这个问题?代码对node.js后端进行rest调用。代码对node.js后端进行rest调用,在db上执行一些查询,直到swift 5实现之前,我没有任何问题,我如何解决这个问题 预期:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“从主线程访问布局引擎后,不得从后台线程对其执行修改。” 查看: Dispatch

您好,自从我将我的应用程序转换为swift 5以来,我在异步调用上出现了一系列错误,这是下面编写的代码中的一个问题,我如何解决这个问题?代码对node.js后端进行rest调用。代码对node.js后端进行rest调用,在db上执行一些查询,直到swift 5实现之前,我没有任何问题,我如何解决这个问题

预期:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“从主线程访问布局引擎后,不得从后台线程对其执行修改。”

查看:

DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
        var MessageError = "Errore"


        if(self.u.PermessoAccessoPosizione() == true) {
            self.u.Marcatura(Longitudine: Float(self.Longitudine)!, Latitudine: Float(self.Latitudine)!, Stato: Stato.description, completion: { result in

                    if(result != "true")
                    {
                        MessageError = "Errore: " + result
                        let alertController = UIAlertController(title: "Errore", message: MessageError, preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                    else {

                        var Message = ""
                        switch Stato {
                        case .Ingresso: Message = "Ingresso registrato"
                        case .Uscita: Message = "Uscita registrata"
                        case .Arrivo: Message = "Arrivo in cantiere"
                        case .Partenza: Message = "Partenza dal cantiere"

                        }

                        let alertController = UIAlertController(title: "MarcaTempo Aggiornato", message: "" + Message, preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                })
        }
        else {
            let alertController = UIAlertController(title: "Errore", message: "Abilita Acessso a posizione", preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(OKAction)
            self.present(alertController, animated: true, completion: nil)
        }
    });
self.ChangeButtonColor(color: UIColor.white)
func Marcatura(Longitudine: Float, Latitudine: Float, Stato: String, completion: @escaping (String) -> ()) {
        DispatchQueue.global(qos: .userInteractive).async {
            let rtemp = UserModel()
            var IdUtenteMarcatura: Int = Int(self.GetIdUtente())
            if(self.IdUtente > 0) {
                IdUtenteMarcatura = self.IdUtente
            }

            rtemp.Marcatura(IdUtente: IdUtenteMarcatura, Longitudine: Longitudine, Latitudine: Latitudine, Stato: Stato, completion: { result in
                completion(result)
            })

            self.IdUtente = 0

        }

    }
func Marcatura(IdUtente: Int, Longitudine: Float, Latitudine: Float, Stato: String, completion: @escaping (String) -> ())
    {
        let jsonrequest = JSON()
        let jsonarray: [String: Any] = ["IdUtente": IdUtente, "Longitudine": Longitudine, "Latitudine": Latitudine, "Stato": Stato]
        jsonrequest.GetSingleString(Router: "/Utente/Marcatura", ValueArray: jsonarray, completion: { result in
            completion(result)
        })
    }
控制器:

DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
        var MessageError = "Errore"


        if(self.u.PermessoAccessoPosizione() == true) {
            self.u.Marcatura(Longitudine: Float(self.Longitudine)!, Latitudine: Float(self.Latitudine)!, Stato: Stato.description, completion: { result in

                    if(result != "true")
                    {
                        MessageError = "Errore: " + result
                        let alertController = UIAlertController(title: "Errore", message: MessageError, preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                    else {

                        var Message = ""
                        switch Stato {
                        case .Ingresso: Message = "Ingresso registrato"
                        case .Uscita: Message = "Uscita registrata"
                        case .Arrivo: Message = "Arrivo in cantiere"
                        case .Partenza: Message = "Partenza dal cantiere"

                        }

                        let alertController = UIAlertController(title: "MarcaTempo Aggiornato", message: "" + Message, preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                })
        }
        else {
            let alertController = UIAlertController(title: "Errore", message: "Abilita Acessso a posizione", preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(OKAction)
            self.present(alertController, animated: true, completion: nil)
        }
    });
self.ChangeButtonColor(color: UIColor.white)
func Marcatura(Longitudine: Float, Latitudine: Float, Stato: String, completion: @escaping (String) -> ()) {
        DispatchQueue.global(qos: .userInteractive).async {
            let rtemp = UserModel()
            var IdUtenteMarcatura: Int = Int(self.GetIdUtente())
            if(self.IdUtente > 0) {
                IdUtenteMarcatura = self.IdUtente
            }

            rtemp.Marcatura(IdUtente: IdUtenteMarcatura, Longitudine: Longitudine, Latitudine: Latitudine, Stato: Stato, completion: { result in
                completion(result)
            })

            self.IdUtente = 0

        }

    }
func Marcatura(IdUtente: Int, Longitudine: Float, Latitudine: Float, Stato: String, completion: @escaping (String) -> ())
    {
        let jsonrequest = JSON()
        let jsonarray: [String: Any] = ["IdUtente": IdUtente, "Longitudine": Longitudine, "Latitudine": Latitudine, "Stato": Stato]
        jsonrequest.GetSingleString(Router: "/Utente/Marcatura", ValueArray: jsonarray, completion: { result in
            completion(result)
        })
    }
型号:

DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
        var MessageError = "Errore"


        if(self.u.PermessoAccessoPosizione() == true) {
            self.u.Marcatura(Longitudine: Float(self.Longitudine)!, Latitudine: Float(self.Latitudine)!, Stato: Stato.description, completion: { result in

                    if(result != "true")
                    {
                        MessageError = "Errore: " + result
                        let alertController = UIAlertController(title: "Errore", message: MessageError, preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                    else {

                        var Message = ""
                        switch Stato {
                        case .Ingresso: Message = "Ingresso registrato"
                        case .Uscita: Message = "Uscita registrata"
                        case .Arrivo: Message = "Arrivo in cantiere"
                        case .Partenza: Message = "Partenza dal cantiere"

                        }

                        let alertController = UIAlertController(title: "MarcaTempo Aggiornato", message: "" + Message, preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(OKAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                })
        }
        else {
            let alertController = UIAlertController(title: "Errore", message: "Abilita Acessso a posizione", preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(OKAction)
            self.present(alertController, animated: true, completion: nil)
        }
    });
self.ChangeButtonColor(color: UIColor.white)
func Marcatura(Longitudine: Float, Latitudine: Float, Stato: String, completion: @escaping (String) -> ()) {
        DispatchQueue.global(qos: .userInteractive).async {
            let rtemp = UserModel()
            var IdUtenteMarcatura: Int = Int(self.GetIdUtente())
            if(self.IdUtente > 0) {
                IdUtenteMarcatura = self.IdUtente
            }

            rtemp.Marcatura(IdUtente: IdUtenteMarcatura, Longitudine: Longitudine, Latitudine: Latitudine, Stato: Stato, completion: { result in
                completion(result)
            })

            self.IdUtente = 0

        }

    }
func Marcatura(IdUtente: Int, Longitudine: Float, Latitudine: Float, Stato: String, completion: @escaping (String) -> ())
    {
        let jsonrequest = JSON()
        let jsonarray: [String: Any] = ["IdUtente": IdUtente, "Longitudine": Longitudine, "Latitudine": Latitudine, "Stato": Stato]
        jsonrequest.GetSingleString(Router: "/Utente/Marcatura", ValueArray: jsonarray, completion: { result in
            completion(result)
        })
    }

你有什么例外?在哪一行?异常跟踪会很好。我在匆忙中添加了异常,我忘了添加它@RobertoMessage是显式的。UI相关的东西只能在主线程中制作。使用
DispatchQueue.main.async{}
。另外,以小写字母命名您的var。我已经删除了DispatchQueue,但错误仍然存在,然后您仍然在后台线程上运行。可能是从后台调用了
Marcatura()
。您必须确保所有UI工作都在主线程上。它可能来自代码的另一部分。异常应该包括导致问题的行。