This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes?

what does it mean

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes ?

i read something about

DispatchQueue.main.async { //code }

i don't understand it quite well

@IBAction func CrearCuenta(_ sender: UIButton) {
        guard
        let nombreConductorTextt = Nombre_Completo.text, !nombreConductorTextt.isEmpty,
        let emailTextt = Correo.text, !emailTextt.isEmpty,
        let passwordTextt = Password.text, !passwordTextt.isEmpty,
        let confirmarpassTextt = passConfirma.text, !confirmarpassTextt.isEmpty,
        let telefonoTextt = Telefono.text, !telefonoTextt.isEmpty,
        let telefonoContactoTextt = Telefono_Contacto.text, !telefonoContactoTextt.isEmpty,
        let emailContactoTextt = Email_Contacto.text, !emailContactoTextt.isEmpty
        else  {
            displayAlert(title: "Información Faltante", message: "Debes porporcionar los datos solicitados")
            return
        }
        guard passwordTextt == confirmarpassTextt else {
            displayAlert(title: "Usuario", message: "Las contraseñas no coinciden")
            return
        }
        var request = URLRequest(url: URL(string: "hehe")!)
        request.httpMethod = "POST"
      
        
        let postParams = [
            "Nombre_Completo": nombreConductorTextt,
            "Correo": emailTextt,
            "Password": passwordTextt,
            "Telefono": telefonoTextt,
            "Email_Contacto": emailContactoTextt,
            "Telefono_Contacto": telefonoContactoTextt,
            
            ]
        let postString = postParams.map {"\($0.key.urlQueryValueEscaped)=\($0.value.urlQueryValueEscaped)"}
            .joined(separator: "&")
        request.httpBody = postString.data(using: .utf8)
        
       
        let task = URLSession.shared.dataTask(with: request) {
            data, response, error in
            
            if let error = error {
                print("error=\(error)")
                self.displayAlert(title: "Error", message: "Trata denuevo")
                return
            }
            
            print("response = \(response?.description ?? "")")
            
            guard let data = data else {
                print("Something wrong: data = nil")
                return
            }
            
           
            let responseString = String(data: data, encoding: .utf8)!
            print("responseString = \(responseString)")
            self.displayAlert(title: "Registro exitoso", message: "¡Usuario creado!")
            self.Nombre_Completo.text = ""
            self.Correo.text = ""
            self.Password.text = ""
            self.passConfirma.text = ""
            self.Telefono.text = ""
            self.Telefono_Contacto.text = ""
            self.Email_Contacto.text = ""
            
            
        }
        task.resume()
        
        
    }


plz any hints?

Replies

It means what it tells : you are updating the display from a background thread.


Where do you get the error message ?


Could be lines 41 or 55.


Could you show the code of displayAlert ?

Do you execute in the main thread with


func displayAlert(title: String, message: String) {
     DispatchQueue.main.async {

     }
}

all code


import Alamofire
import UIKit

class creaciondecuentaViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet var Nombre_Completo: UITextField!
    @IBOutlet var elskrolll: UIScrollView!
    
    var iconClick : Bool!
    
    @IBOutlet var Correo: UITextField!
    
    
    @IBOutlet var Password: UITextField!
    @IBOutlet var passConfirma: UITextField!
    
    
    
    @IBOutlet var Telefono: UITextField!
    
    @IBOutlet var Telefono_Contacto: UITextField!
    
    @IBOutlet var Email_Contacto: UITextField!
    var pasajero = true
    var nivel = "0"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        iconClick = true
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.touch))
        recognizer.numberOfTapsRequired = 1
        recognizer.numberOfTouchesRequired = 1
        elskrolll.addGestureRecognizer(recognizer)
        
        
        
        
    }
    
    @objc func touch() {
     
        self.view.endEditing(true)
        
    }
    @IBAction func CrearCuenta(_ sender: UIButton) {
        guard
            let nombreConductorTextt = Nombre_Completo.text, !nombreConductorTextt.isEmpty,
            let emailTextt = Correo.text, !emailTextt.isEmpty,
            let passwordTextt = Password.text, !passwordTextt.isEmpty,
            let confirmarpassTextt = passConfirma.text, !confirmarpassTextt.isEmpty,
            let telefonoTextt = Telefono.text, !telefonoTextt.isEmpty,
            let telefonoContactoTextt = Telefono_Contacto.text, !telefonoContactoTextt.isEmpty,
            let emailContactoTextt = Email_Contacto.text, !emailContactoTextt.isEmpty
            else  {
                displayAlert(title: "Información Faltante", message: "Debes porporcionar los datos solicitados")
                return
        }
        guard passwordTextt == confirmarpassTextt else {
            displayAlert(title: "Usuario", message: "Las contraseñas no coinciden")
            return
        }
        var request = URLRequest(url: URL(string: "hehe")!)
        request.httpMethod = "POST"

        
        let postParams = [
            "Nombre_Completo": nombreConductorTextt,
            "Correo": emailTextt,
            "Password": passwordTextt,
            "Telefono": telefonoTextt,
            "Email_Contacto": emailContactoTextt,
            "Telefono_Contacto": telefonoContactoTextt,
            
            ]
        let postString = postParams.map {"\($0.key.urlQueryValueEscaped)=\($0.value.urlQueryValueEscaped)"}
            .joined(separator: "&")
        request.httpBody = postString.data(using: .utf8)
        

        let task = URLSession.shared.dataTask(with: request) {
            data, response, error in
            
            if let error = error {
                print("error=\(error)")
                self.displayAlert(title: "Error", message: "Trata denuevo")
                return
            }
            
            print("response = \(response?.description ?? "")")
            
            guard let data = data else {
                print("Something wrong: data = nil")
                return
            }
            

            let responseString = String(data: data, encoding: .utf8)!
            print("responseString = \(responseString)")
            self.displayAlert(title: "Registro exitoso", message: "¡Usuario creado!")
            self.Nombre_Completo.text = ""
            self.Correo.text = ""
            self.Password.text = ""
            self.passConfirma.text = ""
            self.Telefono.text = ""
            self.Telefono_Contacto.text = ""
            self.Email_Contacto.text = ""
            
            
        }
        task.resume()
        
        
    }
    
    
    func displayAlert (title:String, message:String){
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        self.present(alertController, animated: true, completion: nil)
        
    }
    
    @IBAction func verContra(_ sender: UIButton) {
        let userPazzword = Password.text!;
        
        if(iconClick == true) {
            Password.isSecureTextEntry = false
            passConfirma.isSecureTextEntry = false
            iconClick = false
        } else {
            Password.isSecureTextEntry = true
            passConfirma.isSecureTextEntry = true
            iconClick = true
        }
    }
    
    
    
    
   
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.view.endEditing(true)
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    
    
    
    @IBAction func unwindToVC1(segue:UIStoryboardSegue) {
        
    }
    @IBAction func goBackToOneButtonTapped(_ sender: Any) {
        performSegue(withIdentifier: "fromregistroToMain", sender: self)
    }
    
    
    
    
}

my code is working but on the console is printing like crazy

"This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes"




This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
 Stack:(
  0   Foundation                          0x000000022724efc0 <redacted> + 96
  1   Foundation                          0x00000002270414d4 <redacted> + 36
  2   UIKitCore                           0x00000002538baa14 <redacted> + 564
  3   UIKitCore                           0x00000002538ba79c <redacted> + 224
  4   UIKitCore                           0x0000000253961f88 <redacted> + 120
  5   Foundation                          0x000000022704151c <redacted> + 108
  6   UIKitCore                           0x0000000253961e90 <redacted> + 784
  7   UIKitCore                           0x0000000253971bb8 <redacted> + 1740
  8   UIKitCore                           0x0000000253791c4c <redacted> + 328
  9   UIKitCore                           0x000000025377f664 <redacted> + 116
  10  UIKitCore                           0x00000002537dfbc8 <redacted> + 312
  11  CoreFoundation                      0x0000000226610d14 <redacted> + 20
  12  CoreFoundation                      0x0000000226610ce0 <redacted> + 64
  13  CoreFoundation                      0x00000002266101d0 <redacted> + 392
  14  CoreFoundation                      0x000000022660fe7c <redacted> + 96
  15  CoreFoundation                      0x00000002265881c0 <redacted> + 1404
  16  CoreFoundation                      0x000000022660f908 _CFXNotificationPost + 696
  17  Foundation                          0x000000022701eeb0 <redacted> + 68
  18  UIFoundation                        0x0000000230c66248 <redacted> + 256
  19  UIFoundation                        0x0000000230c66b04 <redacted> + 336
  20  Foundation                          0x0000000227075490 <redacted> + 348
  21  UIFoundation                        0x0000000230cdc5f0 <redacted> + 44
  22  UIFoundation                        0x0000000230cdc13c <redacted> + 56
  23  UIFoundation                        0x0000000230c6cb44 <redacted> + 72
  24  UIKitCore                           0x00000002537c8734 <redacted> + 324
  25  123Taxi                             0x0000000100e8a368 $S7_23Taxi30creaciondecuentaViewControllerC11CrearCuentayySo8UIButtonCFyycfU_y10Foundation4DataVSg_So13NSURLResponseCSgs5Error_pSgtcfU0_ + 4068
  26  123Taxi                             0x0000000100e664b8 $S10Foundation4DataVSgSo13NSURLResponseCSgs5Error_pSgIegggg_So6NSDataCSgAGSo7NSErrorCSgIeyByyy_TR + 316
  27  CFNetwork                           0x0000000226c7c7bc <redacted> + 32
  28  CFNetwork                           0x0000000226c90f5c <redacted> + 176
  29  Foundation                          0x000000022712fb6c <redacted> + 16
  30  Foundation                          0x0000000227037cc8 <redacted> + 72
  31  Foundation                          0x000000022703719c <redacted> + 740
  32  Foundation                          0x0000000227131a40 <redacted> + 272
  33  libdispatch.dylib                   0x0000000101fa7840 _dispatch_call_block_and_release + 24
  34  libdispatch.dylib                   0x0000000101fa8de4 _dispatch_client_callout + 16
  35  libdispatch.dylib                   0x0000000101fac1e0 _dispatch_continuation_pop + 528
  36  libdispatch.dylib                   0x0000000101fab620 _dispatch_async_redirect_invoke + 632
  37  libdispatch.dylib                   0x0000000101fba1f8 _dispatch_root_queue_drain + 376
  38  libdispatch.dylib                   0x0000000101fbabe4 _dispatch_worker_thread2 + 156
  39  libsystem_pthread.dylib             0x00000002262be190 _pthread_wqthread + 472
  40  libsystem_pthread.dylib             0x00000002262c0d00 start_wqthread + 4
)

Did you try to change :


    func displayAlert (title:String, message:String){ 
          DispatchQueue.main.async {
             let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
             alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) 
             self.present(alertController, animated: true, completion: nil) 
          }
    }

That’s not going to completely solve the problem, since he’s also assigning to the text property of a bunch of UITextFields. That also has to be done on the main thread. The underlying problem, I think, is that he needs to learn what threads are and how to work with them. Providing code to paste in isn’t going to fix that.