Hello the following code represents an MVC that deals with the creation of an element inside a db with a rest service in node.js, when I execute the code I get the following error: being called from a background thread, how can I solve this?
Error: being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior.
/*View Code */
@objc func CreazioneCantiere(sender: UIButton!) {
//Creazione Cantiere a Consuntivo
if(TipologiaCantiereSelezionata == 0) {
if(self.txtNomeCantiere.text != "") {
let cantieretemp = Cantiere(Cliente: self.c!)
cantieretemp.Crea(NomeCantiere: self.txtNomeCantiere.text!, completion: { result in
print("Terminata async nella view")
//sleep(2)
if(result == true)
{
print("View con result a true")
let CantiereSelezionato = GestioneCantieriViewController(CantiereSet: cantieretemp)
self.present(CantiereSelezionato, animated: true, completion: nil
)
}
else
{
print("View con result a false")
let alertController = UIAlertController(title: "Errore", message: "Creazione Cantiere non riuscita", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
})
}
}
/*Controller*/
func Crea(NomeCantiere: String, completion: @escaping (Bool) -> Void) {
DispatchQueue.global(qos: .userInteractive).async {
print("Funzione crea Cantiere nel Controller")
self.CantiereInterno.NomeCantiere = NomeCantiere
self.CantiereInterno.IdCliente = self.ClienteCantiere.GetIdCliente()
self.CantiereInterno.Filiale = "SEDE"
let cmodel = CantiereModel()
cmodel.CreazioneCantiere(IdCliente: self.ClienteCantiere.GetIdCliente(), NomeCantiere: NomeCantiere,
completion: {
result in
self.inizializza(SetCantiere: result[0] as CantiereStruct)
print("Funzione crea Cantiere nel Controller inizializzazione ")
completion(true)
})
print("Funzione crea Cantiere nel Controller fine")
}
}
}
/* Model */
func CreazioneCantiere(IdCliente: Int, NomeCantiere: String, completion: @escaping ([CantiereStruct]) -> ())
{
DispatchQueue.main.async {
print("Funzione Creazione Cantiere nel model")
let jsonrequest = JSON()
let jsonarray: [String: Any] = ["NomeCantiere": NomeCantiere, "IdCliente": IdCliente, "IdUtente": GetIdUtenteLogin()]
jsonrequest.GetArray(Tipo: "Cantiere", Router: "/cantieri/generacantiere", ValueArray: jsonarray, completion: { result in
completion(result as! [CantiereStruct])
})
print("Terminata la funzionedi creazione Cantiere nel model")
}
}